Implements Statement

Specifies one or more interfaces, or interface members, that must be implemented in the class or structure definition in which it appears.

Syntax

Implements interfacename [, ...]  
' -or-  
Implements interfacename.interfacemember [, ...]  

Parts

interfacename
Required. An interface whose properties, procedures, and events are to be implemented by corresponding members in the class or structure.

interfacemember
Required. The member of an interface that is being implemented.

Remarks

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. For more information, see Interfaces.

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 the Implements keyword (which is separate from the Implements statement) when you declare the member in the class or structure. For more information, see Interfaces.

Classes can use Private implementations 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.

Example 1

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.

Example 2

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

See also