Export (0) Print
Expand All

Walkthrough: Creating and Implementing Interfaces

Interfaces describe the characteristics of properties, methods, and events, but leave the implementation details up to structures or classes.

This walkthrough demonstrates how to declare and implement an interface.

To define an interface

  1. Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
  2. Select Visual Basic Projects in the Project Types pane.
  3. Select Windows Application from the list of Visual Basic project templates.
  4. Add a new module to the project by clicking Add Module on the Project menu.
  5. Name the new module Module1.vb and click Open. The code for the new module is displayed.
  6. Define an interface named MyInterface within Module1 by typing Interface MyInterface between the Module and End Module statements and then pressing ENTER. The code editor indents the Interface keyword and adds an End Interface statement to form a code block.
  7. Define a property, method, and event for the interface by placing the following code between the Interface and End Interface statements:
       Property Prop1() As Integer
       Sub Method1(ByVal X As Integer)
       Event Event1()
    

You may notice that the syntax used to declare interface members is different from the syntax used to declare class members. This difference reflects the fact that interfaces cannot contain implementation code.

To implement the interface

  1. Add a class named ImplementationClass by adding the following statement to Module1 after the End Interface statement but before the End Module statement and then pressing ENTER:
    Class ImplementationClass
    

    If you are working within the integrated development environment, the code editor will supply a mathching End Class statement when you press ENTER.

  2. Add the following Implements statement to ImplementationClass, which names the interface the class implements:
    Implements MyInterface
    

    When listed separately from other items at the top of a class or structure, the Implements statement indicates that the class or structure implements an interface. If you are working within the integrated development environment, the Code Editor may underline the word MyInterface and warn you that this class must implement all the members of the interface MyInterface. The next two steps in this walkthrough implement these members and take care of this warning.

  3. Add the following code to ImplementationClass to implement Event1.
    Event Event1() Implements MyInterface.Event1
    

    This code contains an Event statement to implement Event1, and an Implements statement that names the interface and interface member being implemented.

  4. Add the following code to ImplementationClass to implement the remaining members of the interface:
    Private pval As Integer ' Holds the value of the property.
    Property Prop1() As Integer Implements MyInterface.Prop1
       Get
          Return pval
       End Get
       Set(ByVal Value As Integer)
          pval = Value
       End Set
    End Property
    
    Sub Method1(ByVal X As Integer) Implements MyInterface.Method1
       MsgBox("The X parameter for Method1 is " & X)
       RaiseEvent Event1()
       ' Add more code to implement this method.
    End Sub
    

    For each member being implemented in this code, an Implements statement indicates the name of the interface and member being implemented.

To test the implementation of the interface

  1. Right click the startup form for your project in the Solution Explorer and click View Code. The editor displays the class for your startup form. By default the startup form is called Form1.
  2. Add the following event handler to the Form1 class to handle events raised by instances of ImplementationClass:
    Sub EventHandler()
       MsgBox("The event handler caught the event.")
    End Sub
    
  3. Add a subroutine named Test to the Form1 class to test the implementation class:
    Sub Test()
    '  Create an instance of the class.
       Dim T As New ImplementationClass()
    '  Create a variable of type MyInterface.
       Dim I As MyInterface
    '  Assign the class instance to the interface.
    '  Calls to the interface members are 
    '  executed through the class instance.
       I = T
    '  Associate an event handler with an event raised by the object.
       AddHandler I.Event1, AddressOf EventHandler
       I.Prop1 = 9 'Set a property.
       MsgBox("Prop1 was set to " & I.Prop1) ' Read the property.
    '  Test the method and raise an event.
       I.Method1(5)
    End Sub
    

    The Test procedure creates an instance of the class that implements MyInterface, creates a variable of the interface's type, associates an event handler with the event raised by the instance, sets a property, and runs a method through the interface.

  4. Add code to call the Test procedure from the Form1_Load procedure of your startup form:
    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load
       Test() ' Test the class.
    End Sub
    
  5. Run the Test procedure by pressing F5. The message "Prop1 was set to 9" is displayed. After you click OK, the message "The X parameter for Method1 is 5" is displayed. Click OK and the message "The event handler caught the event" is displayed.

See Also

Implements Statement | Interfaces in Visual Basic .NET | When to Use Interfaces | Creating and Implementing an Interface | Interface Statement | Event Statement

Show:
© 2014 Microsoft