Specifies one or more interfaces, or interface members, that must be implemented in the class or structure definition in which it appears.
An interface is a collection of prototypes representing the members (properties, procedures, and events) the interface encapsulates. Interfaces contain only the declarations for members; classes and structures implement these members.
The Implements statement must immediately follow the Class or Structure statement.
When you implement an interface, you must implement all the members declared in the interface. Omitting any member is considered to be a syntax error. To implement an individual member, you specify thekeyword (which is separate from the Implements statement) when you declare the member in the class or structure. For more information, see .
Classes can useimplementations of properties and procedures, but these members are accessible only by casting an instance of the implementing class into a variable declared to be of the type of the interface.
The following example shows how to use the Implements statement to implement members of an interface. It defines an interface named ICustomerInfo with an event, a property, and a procedure. The class customerInfo implements all the members defined in the interface.
Public Interface ICustomerInfo Event updateComplete() Property customerName() As String Sub updateCustomerStatus() End Interface Public Class customerInfo Implements ICustomerInfo ' Storage for the property value. Private customerNameValue As String Public Event updateComplete() Implements ICustomerInfo.updateComplete Public Property CustomerName() As String Implements _ ICustomerInfo.customerName Get Return customerNameValue End Get Set(ByVal value As String) ' The value parameter is passed to the Set procedure ' when the contents of this property are modified. customerNameValue = value End Set End Property Public Sub updateCustomerStatus() Implements _ ICustomerInfo.updateCustomerStatus ' Add code here to update the status of this account. ' Raise an event to indicate that this procedure is done. RaiseEvent updateComplete() End Sub End Class
Note that the class customerInfo uses the Implements statement on a separate source code line to indicate that the class implements all the members of the ICustomerInfo interface. Then each member in the class uses the Implements keyword as part of its member declaration to indicate that it implements that interface member.
The following two procedures show how you could use the interface implemented in the preceding example. To test the implementation, add these procedures to your project and call the testImplements procedure.
Public Sub testImplements() ' This procedure tests the interface implementation by ' creating an instance of the class that implements ICustomerInfo. Dim cust As ICustomerInfo = New customerInfo() ' Associate an event handler with the event that is raised by ' the cust object. AddHandler cust.updateComplete, AddressOf handleUpdateComplete ' Set the customerName Property cust.customerName = "Fred" ' Retrieve and display the customerName property. MsgBox("Customer name is: " & cust.customerName) ' Call the updateCustomerStatus procedure, which raises the ' updateComplete event. cust.updateCustomerStatus() End Sub Sub handleUpdateComplete() ' This is the event handler for the updateComplete event. MsgBox("Update is complete.") End Sub