Istruzione Implements

Specifica una o più interfacce, o membri dell'interfaccia, che devono essere implementate nella definizione della classe o della struttura in cui viene visualizzata.

Sintassi

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

Parti

interfacename
Obbligatorio. Un’interfaccia le cui proprietà, routine ed eventi devono essere implementate dai membri corrispondenti nella classe o nella struttura.

interfacemember
Obbligatorio. Membro di un'interfaccia implementata.

Osservazioni:

Un'interfaccia è una raccolta di prototipi che rappresentano i membri (proprietà, routine ed eventi) incapsulati dall'interfaccia. Le interfacce contengono solo le dichiarazioni per i membri; le classi e le strutture implementano questi membri. Per altre informazioni, vedere Interfacce.

L'istruzione Implements deve seguire immediatamente l'istruzione Class o Structure.

Quando si implementa un'interfaccia, è necessario implementare tutti i membri dichiarati nell'interfaccia. L'omissione di qualsiasi membro è considerata un errore di sintassi. Per implementare un singolo membro, specificare la parola chiave Implements (separata dall'istruzione Implements) quando si dichiara il membro nella classe o nella struttura. Per altre informazioni, vedere Interfacce.

Le classi possono usare implementazioni private di proprietà e routine, ma questi membri sono accessibili solo eseguendo il cast di un'istanza della classe di implementazione in una variabile dichiarata come del tipo dell'interfaccia.

Esempio 1

Nell'esempio seguente viene illustrato come usare l'istruzione Implements per implementare i membri di un'interfaccia. Definisce un'interfaccia denominata ICustomerInfo con un evento, una proprietà e una routine. La classe customerInfo implementa tutti i membri definiti nell'interfaccia.

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

Si noti che la classe customerInfo usa l'istruzione Implements in una riga di codice sorgente separata per indicare che la classe implementa tutti i membri dell'interfaccia ICustomerInfo. Ogni membro della classe usa quindi la parola chiave Implements come parte della dichiarazione del membro per indicare che implementa tale membro di interfaccia.

Esempio 2

Nelle due procedure seguenti viene illustrato come usare l'interfaccia implementata nell'esempio precedente. Per testare l'implementazione, aggiungere queste routine al progetto e chiamare la routine testImplements.

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

Vedi anche