O'Reilly logo

Learning Visual Basic .NET by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing an Interface

Suppose you are the author of a Document class, which specifies that Document objects can be stored in a database. You decide to have Document implement the IStorable interface. It isn't required that you do so, but by implementing the IStorable interface you signal to potential clients that the Document class can be used just like any other IStorable object. This will, for example, allow your clients to add your Document objects to a collection of IStorable objects, and to otherwise interact with your Document in this very general and well-understood way.

To implement the IStorable interface, you must do two things:

  1. Declare that a particular class implements the interface, using the Implements keyword. The following code declares that the Document class implements IStorable:

    Public Class Document
    Implements IStorable
  2. Implement each of the interface methods, events, properties, and so forth, and explicitly mark each member as implementing the corresponding interface member. The following code would implement the IStorable interface's Read( ) method:

    Public Sub Read( ) Implements IStorable.Read
        Console.WriteLine("Implementing the Read Method for IStorable")
    End Sub 'Read

Visual Studio .NET will assist you in this effort through IntelliSense. When you enter the keyword Implements, IntelliSense prompts you with the various interfaces, as shown in Figure 13-1.

Figure 13-1. IntelliSense helps with Implements

Once you enter the name of the interface, IntelliSense ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required