Implements ステートメント

クラスまたは構造体の定義に実装する必要のある、1 つ以上のインターフェイス (インターフェイス メンバー) を指定します。

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

指定項目

  • interfacename
    必ず指定します。 インターフェイスを指定します。このインターフェイスのプロパティ、プロシージャ、およびイベントが、クラスまたは構造体の対応するメンバーによって実装されます。

  • interfacemember
    必ず指定します。 実装されるインターフェイスのメンバーを指定します。

解説

インターフェイスは、インターフェイスによってカプセル化されるメンバー (プロパティ、プロシージャ、およびイベント) を表すプロトタイプの集合体です。 インターフェイスには、メンバーの宣言だけが含まれます。これらのメンバーを実装するのは、クラスおよび構造体です。

Implements ステートメントは、Class ステートメントまたは Structure ステートメントの直後に指定する必要があります。

インターフェイスを実装するときは、インターフェイスで宣言されたメンバーをすべて実装する必要があります。 メンバーのいずれかを省略すると、構文エラーと見なされます。 個々のメンバーを実装するには、クラスまたは構造体にメンバーを宣言するときに、Implements 句 (Visual Basic) キーワードを (Implements ステートメントとは別に) 指定します。 詳細については、「インターフェイス (Visual Basic)」を参照してください。

クラスは、プロパティおよびプロシージャを Private (Visual Basic) で実装できますが、そうするとクラスのインスタンスをインターフェイスの型で宣言した変数にキャストしない限り、これらのメンバーにアクセスできなくなります。

使用例

Implements ステートメントを使って、インターフェイスのメンバーを実装するコード例を次に示します。 イベント、プロパティ、およびプロシージャを含むインターフェイスが、ICustomerInfo という名前で定義されています。 customerInfo クラスは、インターフェイスに定義されたすべてのメンバーを実装します。

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

customerInfo クラスでは 1 行のソース コードに Implements ステートメントを定義し、そのクラスが ICustomerInfo インターフェイスのすべてのメンバーを実装することを示しています。 その後、クラスの各メンバーにおいて、メンバー宣言に Implements キーワードを指定し、そのインターフェイスのメンバーが実装されることを示しています。

実装されたインターフェイスを使用する 2 つのプロシージャを次に示します。 インターフェイスの実装をテストするには、これらのプロシージャをプロジェクトに追加して、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

参照

参照

Implements 句 (Visual Basic)

Interface ステートメント (Visual Basic)

概念

インターフェイス (Visual Basic)