|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Specifies one or more interfaces, or interface members, that will be implemented in the class or structure definition in which it appears.
Implements interfacename [, ...]
Implements interfacename.interfacemember [, ...]
- Required. An interface whose properties, methods, and events will be implemented by corresponding members in the class or structure.
- Required. The member of an interface that is being implemented.
An interface is a collection of prototypes representing the members (properties, methods and events) the interface encapsulates. Interfaces contain only the declarations for members; classes and structures implement these members.
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. Classes can use Private implementations of properties and methods, but these members are only accessible 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 the properties, methods and events of an interface.
This example defines an interface named ICustomerInfo with a property, method, and an event. The Class CustomerInfo implements all the members defined in the interface.
Interface IcustomerInfo ' Declare an interface. Property CustomerName() As String Sub UpdateCustomerStatus() Event UpdateComplete() End Interface Public Class CustomerInfo ' The CustomerInfo class implements the IcustomerInfo interface. Implements ICustomerInfo Private CustomerNameValue As String ' Used to store the property value. 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 is modified. CustomerNameValue = Value ' Save the new 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 method is done. RaiseEvent UpdateComplete() End Sub End Class
The following two procedures show how you could use the implemented interface. To test the interface implementation, add these procedures to your project and call the
Public Sub TestImplements() ' This procedure tests the interface implementation by ' creating an instance of the class that implements ICustomerInfo. Dim Cust As 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 update CustomerStatus method that ' 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