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
- Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
- Select Visual Basic Projects in the Project Types pane.
- Select Windows Application from the list of Visual Basic project templates.
- Add a new module to the project by clicking Add Module on the Project menu.
- Name the new module
Module1.vband click Open. The code for the new module is displayed.
- Define an interface named
Interface MyInterfacebetween 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.
- 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
- Add a class named
ImplementationClassby adding the following statement to
End Interfacestatement but before the
End Modulestatement and then pressing ENTER:
If you are working within the integrated development environment, the code editor will supply a mathching
End Classstatement when you press ENTER.
- Add the following Implements statement to
ImplementationClass, which names the interface the class implements:
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
MyInterfaceand 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.
- Add the following code to
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.
- Add the following code to
ImplementationClassto 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
- 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
- Add the following event handler to the
Form1class to handle events raised by instances of
Sub EventHandler() MsgBox("The event handler caught the event.") End Sub
- Add a subroutine named
Form1class 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
Testprocedure 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.
- Add code to call the
Testprocedure from the
Form1_Loadprocedure 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
- Run the
Testprocedure 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.