共用方式為


Walkthrough: Creating and Implementing Interfaces (Visual Basic)

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.

Note

This walkthrough doesn't provide information about how to create a user interface.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings.

To define an interface

  1. Open a new Visual Basic Windows Application project.

  2. Add a new module to the project by clicking Add Module on the Project menu.

  3. Name the new module Module1.vb and click Add. The code for the new module is displayed.

  4. Define an interface named TestInterface within Module1 by typing Interface TestInterface 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.

  5. 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()
    

Implementation

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 supplies a matching End Class statement when you press ENTER.

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

    Implements TestInterface
    

    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 implements the class members required by TestInterface when you press ENTER, and you can skip the next step.

  3. If you are not working within the integrated development environment, you must implement all the members of the interface MyInterface. Add the following code to ImplementationClass to implement Event1, Method1, and Prop1:

    Event Event1() Implements TestInterface.Event1
    
    Public Sub Method1(ByVal X As Integer) Implements TestInterface.Method1
    End Sub 
    
    Public Property Prop1() As Integer Implements TestInterface.Prop1
        Get 
        End Get 
        Set(ByVal value As Integer)
        End Set 
    End Property
    

    The Implements statement names the interface and interface member being implemented.

  4. Complete the definition of Prop1 by adding a private field to the class that stored the property value:

    ' Holds the value of the property. 
    Private pval As Integer
    

    Return the value of the pval from the property get accessor.

    Return pval
    

    Set the value of pval in the property set accessor.

    pval = value
    
  5. Complete the definition of Method1 by adding the following code.

    MsgBox("The X parameter for Method1 is " & X)
    RaiseEvent Event1()
    

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 testInstance field to the Form1 class:

    Dim WithEvents testInstance As TestInterface
    

    By declaring testInstance as WithEvents, the Form1 class can handle its events.

  3. Add the following event handler to the Form1 class to handle events raised by testInstance:

    Sub EventHandler() Handles testInstance.Event1
        MsgBox("The event handler caught the event.")
    End Sub
    
  4. 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
        ' Assign the class instance to the interface. 
        ' Calls to the interface members are  
        ' executed through the class instance.
        testInstance = T
        ' Set a property.
        testInstance.Prop1 = 9
        ' Read the property.
        MsgBox("Prop1 was set to " & testInstance.Prop1)
        '  Test the method and raise an event.
        testInstance.Method1(5)
    End Sub
    

    The Test procedure creates an instance of the class that implements MyInterface, assigns that instance to the testInstance field, sets a property, and runs a method through the interface.

  5. 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
    
  6. 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

Reference

Implements Statement

Interface Statement (Visual Basic)

Event Statement

Other Resources

Interfaces (Visual Basic)