Skip to Content
Programming Visual Basic .NET, Second Edition
book

Programming Visual Basic .NET, Second Edition

by Jesse Liberty
April 2003
Intermediate to advanced
560 pages
14h 4m
English
O'Reilly Media, Inc.
Content preview from Programming Visual Basic .NET, Second Edition

Implementing an Interface

Suppose you are the author of a Document class that 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 a particular class that implements the interface, using the Implements keyword. The following code declares that the Document class implements IStorable:

    Public Class Document
           Implements IStorable

    Note

    The colon operator allows you to put two statements on a single line. It is not uncommon to write:

    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

Tip

Note that with Sub and Function, the Implements keyword goes on the same line as the method definition, and so no colon is needed.

Visual Studio ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Visual Basic .NET

Programming Visual Basic .NET

Dave Grundgeiger
VB.NET Language in a Nutshell, Second Edition

VB.NET Language in a Nutshell, Second Edition

Steven Roman PhD, Ron Petrusha, Paul Lomax

Publisher Resources

ISBN: 0596004389Supplemental ContentCatalog PageErrata