![]() ![]() | ||
As discussed in the In Depth section of this chapter, interfaces can act as specifications for class members; when you implement an interface, you also must implement all the specified members. There's an example on the CD-ROM named Interfaces that shows how this works; in this case, I create an interface named person that specifies two members: SetName and GetName. Then I implement that interface in a class named employee with the Implements keyword (which must come after Inherits statements and before any Dim statements):
Public Interface person Sub SetName(ByVal PersonName As String) Function GetName() As String End Interface Public Class employee Implements person Dim Name As String Sub SetName(ByVal PersonName As String) Implements person.SetName Name = PersonName End Sub Function GetName() As String Implements person.GetName Return Name End Function End Class
Now I can create an object of the employee class and use it in code, as you see in the Interfaces example:
Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim Edward As New employee() Edward.SetName("Edward") TextBox1.Text = "You created " & Edward.GetName() End Sub End Class
You can see the results of this code in Figure 12.3.
![]() ![]() | ||