Implementing an Interface

Imagine that you are the author of a Document class. It turns out that Document objects can be stored in a database, so you decide to have Document implement the IStorable interface.

To do so, you must do two things:

  • Declare the class to implement the interface.

  • Implement each of the interface methods, events, properties, and so forth, and explicitly mark each as implementing the corresponding interface member.

To declare that the class implements the interface, you use the keyword Implements on the line below the class definition:

Public Class Document
    Implements IStorable

Visual Studio 2005 will help you with the available interfaces , as shown in Figure 18-7.

As soon as you write that your class implements the IStorable interface, Visual Studio 2005 will fill in all the required methods and properties of the interface, as shown in Figure 18-8!

IntelliSense helping find the interface

Figure 18-7. IntelliSense helping find the interface

IntelliSense stubs out all the interface methods and properties

Figure 18-8. IntelliSense stubs out all the interface methods and properties

All you have to do is fill in the stubbed out methods and properties and the Interface contract is fulfilled.

You cannot create an instance of an interface; instead you instantiate a class that implements the interface. The class implementing the interface must fulfill the contract exactly and ...

Get Programming Visual Basic 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.