Export (0) Print
Expand All

7.2 Interface Implementation

Visual Studio .NET 2003

Structure and class declarations may declare that they implement a set of interface types through one or more Implements clauses. All the types specified in the Implements clause must be interfaces, and the type must implement all members of the interfaces. For example:

Interface ICloneable
    Function Clone() As Object
End Interface 

Interface IComparable
    Function CompareTo(ByVal other As Object) As Integer
End Interface 

Structure ListEntry
    Implements ICloneable, IComparable

    Public Function Clone() As Object Implements ICloneable.Clone
    End Function 

    Public Function CompareTo(ByVal other As Object) As Integer _
        Implements IComparable.CompareTo
    End Function 
End Structure

A type that implements an interface also implicitly implements all of the interface's base interfaces. This is true even if the type does not explicitly list all base interfaces in the Implements clause. In this example, the TextBox structure implements both IControl and ITextBox.

Interface IControl
    Sub Paint()
End Interface 

Interface ITextBox
    Inherits IControl
    Sub SetText(ByVal Text As String)
End Interface 

Structure TextBox
    Implements ITextBox

    Public Sub Paint() Implements ITextBox.Paint
    End Sub

    Public Sub SetText(ByVal Text As String) Implements ITextBox.SetText
    End Sub
End Structure

Declaring that a type implements an interface in and of itself does not declare anything in the declaration space of the type. Thus, it is valid to implement two interfaces with a method by the same name.

TypeImplementsClause ::= Implements Implements LineTerminator
Implements ::=
   TypeName |
   Implements , TypeName

See Also

7.5.1 Class Base Specification | 7.5.2 Class Members | 7.5 Classes | 7.6.1 Structure Members | 7.6 Structures | Implements Statement (Visual Basic Language Reference) | Implements Keyword (Visual Basic Language Concepts)

Show:
© 2014 Microsoft