Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
9.1 Interface Method Implementation

9.1 Interface Method Implementation

Visual Studio .NET 2003

Methods, events, and properties can implement interface members. To implement an interface member, a member declaration specifies the Implements keyword and lists one or more interface members. Methods and properties that implement interface members are implicitly NotOverridable unless declared to be MustOverride, Overridable, or overriding another member. It is an error for a member implementing an interface member to be Shared. A member's accessibility has no effect on its ability to implement interface members.

For an interface implementation to be valid, the implements list of the containing type must name an interface that contains a compatible member. A compatible member is one whose signature matches the signature of the implementing member. If an event declared using a delegate type is implementing an interface event, then a compatible event is one whose underlying delegate type is the same type. Otherwise, the event uses the delegate type from the interface event it is implementing. If such an event implements multiple interface events, all the interface events must have the same underlying delegate type. For example:

Interface ClickEvents
    Event LeftClick(ByVal x As Integer, ByVal y As Integer)
    Event RightClick(ByVal x As Integer, ByVal y As Integer)
End Interface

Class Button
    Implements ClickEvents

    ' OK. Signatures match, delegate type = ClickEvents.LeftClickHandler.
    Event LeftClick(ByVal x As Integer, ByVal y As Integer) _
        Implements ClickEvents.LeftClick

    ' OK. Signatures match, delegate type = ClickEvents.RightClickHandler.
    Event RightClick(ByVal x As Integer, ByVal y As Integer) _
        Implements ClickEvents.RightClick
End Class

Class Label
    Implements ClickEvents

    ' Error. Signatures match, but can't be both delegate types.
    Event Click(ByVal x As Integer, ByVal y As Integer) _
        Implements ClickEvents.LeftClick, ClickEvents.RightClick
End Class

An interface member in the implements list is specified using a type name, a period, and an identifier. The type name must be an interface in the implements list or a base interface of an interface in the implements list, and the identifier must be a member of the specified interface. A single member can implement more than one matching interface member.

Interface ILeft
    Sub F()
End Interface

Interface IRight
    Sub F()
End Interface

Class Test
    Implements ILeft, IRight

    Sub F() Implements ILeft.F, IRight.F
    End Sub
End Class

If the interface member being implemented is unavailable in all explicitly implemented interfaces because of multiple interface inheritance, the implementing member must explicitly reference a base interface on which the member is available. For example, if I1 and I2 contain a member M, and I3 inherits from I1 and I2, a type implementing I3 will implement I1.M and I2.M. If an interface shadows multiply inherited members, an implementing type will have to implement the inherited members and the member(s) shadowing them.

Interface ILeft
    Sub F()
End Interface

Interface IRight
    Sub F()
End Interface

Interface ILeftRight
    Inherits ILeft, IRight

    Shadows Sub F()
End Interface

Class Test
    Implements ILeftRight

    Sub LeftF() Implements ILeft.F
    End Sub

    Sub RightF() Implements IRight.F
    End Sub

    Sub LeftRightF() Implements ILeftRight.F
    End Sub
End Sub
ImplementsClause ::= [ Implements ImplementsList ]
ImplementsList ::=
   InterfaceMemberSpecifier |
   ImplementsList , InterfaceMemberSpecifier
InterfaceMemberSpecifier ::= TypeName . Identifier

See Also

9.2.1 Regular Method Declarations | 9.2.2 External Method Declarations | 9.2.3 Overridable Methods |9.2.4 Shared Methods | 9.2.5 Method Parameters | 9.2.6 Event Handling | 9.2 Methods | Implements Statement (Visual Basic Language Reference) | Implements Keyword (Visual Basic Language Concepts)

Show:
© 2015 Microsoft