Section 13.2. Implementing an Interface

   

13.2 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
figs/lvbn_1301.gif

Once you enter the name of the interface, IntelliSense can help you identify which member you are implementing, as shown in Figure 13-2.

Figure 13-2. Choosing a method from an interface
figs/lvbn_1302.gif

Your definition of this class might look like this:

 Public Class Document    Implements IStorable        Public Sub Read( ) Implements IStorable.Read    '...    End Sub 'Read        Public Sub Write(ByVal o As Object) Implements IStorable.Write        '...    End Sub 'Write        Public Property Status( ) As Integer Implements IStorable.Status    '...    End Property End Class 'Document 

It is now your responsibility, as the author of the Document class, to provide a meaningful implementation of the IStorable methods and property. Having designated Document as implementing IStorable, you must implement all the IStorable members , or you will generate an error when you compile. Defining and implementing the IStorable interface is illustrated in Example 13-1.

Example 13-1. Document class implementing IStorable
 Option Strict On Imports System Namespace InterfaceDemo  ' define the interface   Interface IStorable   Sub Read( )   Sub Write(ByVal obj As Object)   Property Status( ) As Integer   End Interface 'IStorable  ' create a class that implements the IStorable interface     Public Class Document         Implements IStorable         Public Sub New(ByVal s As String)             Console.WriteLine("Creating document with: {0}", s)         End Sub 'New  ' implement the Read method   Public Sub Read( ) Implements IStorable.Read   Console.WriteLine("Implementing the Read Method for IStorable")   End Sub 'Read    ' implement the Write method   Public Sub Write(ByVal o As Object) Implements IStorable.Write   Console.WriteLine( _   "Implementing the Write Method for IStorable")   End Sub 'Write    ' implement the property   Public Property Status( ) As Integer Implements IStorable.Status   Get   Return myStatus   End Get   Set(ByVal Value As Integer)   myStatus = Value   End Set   End Property  ' store the value for the property         Private myStatus As Integer = 0     End Class 'Document     Class Tester         Public Sub Run( )             Dim doc As New Document("Test Document")             doc.Status = -1             doc.Read( )             Console.WriteLine("Document Status: {0}", doc.Status)         End Sub 'Run         Public Shared Sub Main( )             Dim t As New Tester( )             t.Run( )         End Sub 'Main     End Class 'Tester End Namespace 'InterfaceDemo 
  Output:  Creating document with: Test Document Implementing the Read Method for IStorable Document Status: -1 

Example 13-1 defines a simple interface, IStorable, with two methods, Read( ) and Write( ), and a property, Status, of type integer:

 ' define the interface     Interface IStorable         Sub Read( )         Sub Write(ByVal obj As Object)         Property Status( ) As Integer     End Interface 'IStorable 

Notice that the IStorable method declarations for Read( ) and Write( ) do not include access modifiers, as was explained earlier, because interface methods need to be public so that they can be used by other classes.

Once you've defined the IStorable interface, you can define classes that implement the interface. Keep in mind that 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 completely. Thus, your Document class must provide both a Read( ) and a Write( ) method and the Status property.

 ' create a class which implements the IStorable interface     Public Class Document         Implements IStorable         Public Sub New(ByVal s As String)             Console.WriteLine("Creating document with: {0}", s)         End Sub 'New                  ' implement the Read method         Public Sub Read( ) Implements IStorable.Read             Console.WriteLine("Implementing the Read Method for IStorable")         End Sub 'Read                  ' implement the Write method         Public Sub Write(ByVal o As Object) Implements IStorable.Write             Console.WriteLine( _                "Implementing the Write Method for IStorable")         End Sub 'Write                  ' implement the property         Public Property Status( ) As Integer Implements IStorable.Status             Get                 Return myStatus             End Get             Set(ByVal Value As Integer)                 myStatus = Value             End Set         End Property                  ' store the value for the property         Private myStatus As Integer = 0     End Class 'Document 

How your Document class fulfills the requirements of the interface, however, is entirely up to you. Although IStorable dictates that Document must have a Status property, it does not know or care whether Document stores the actual status as a member variable or looks it up in a database. Example 13-1 implements the Status property by returning (or setting) the value of a private member variable, status.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net