Share via


Istruzione Implements

Specifica una o più interfacce o membri di interfaccia che è necessario implementare nella definizione di classe o di struttura in cui è inserita.

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

Parti

  • interfacename
    Obbligatorio.Interfaccia le cui proprietà, routine ed eventi devono essere implementati dai membri corrispondenti nella classe o nella struttura.

  • interfacemember
    Obbligatorio.Membro di un'interfaccia da implementare.

Note

Un'interfaccia è una raccolta di prototipi che rappresentano i membri (proprietà, routine ed eventi) in essa contenuti.Le interfacce contengono solo le dichiarazioni dei membri mentre le classi e le strutture li implementano.Per ulteriori informazioni, vedere Interfacce (Visual Basic).

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

Quando si implementa un'interfaccia, è necessario implementare tutti i membri in essa dichiarati.L'omissione di un membro viene considerata un errore di sintassi.Per implementare un singolo membro, specificare la parola chiave Clausola Implements (Visual Basic), distinta dall'istruzione Implements, quando si dichiara il membro nella classe o nella struttura.Per ulteriori informazioni, vedere Interfacce (Visual Basic).

Le classi possono utilizzare le implementazioni Private (Visual Basic) di proprietà e routine, ma è possibile accedere a questi membri solo eseguendo il casting di un'istanza della classe che implementa in una variabile dichiarata come tipo dell'interfaccia.

Esempio

Nell'esempio seguente, viene illustrato come utilizzare 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 utilizza l'istruzione Implements in una riga del codice sorgente distinta per indicare l'implementazione di tutti i membri dell'interfaccia ICustomerInfo.Ogni membro della classe, quindi, include la parola chiave Implements nella dichiarazione dei relativi membri per indicare l'implementazione di quel membro dell'interfaccia.

Nelle seguenti due routine viene illustrato un possibile utilizzo dell'interfaccia implementata nell'esempio precedente.Per eseguire il test dell'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

Vedere anche

Riferimenti

Clausola Implements (Visual Basic)

Istruzione Interface (Visual Basic)

Altre risorse

Interfacce (Visual Basic)