This documentation is archived and is not being maintained.

7.8 Interfaces

Visual Studio .NET 2003

Interfaces are reference types that other types implement to guarantee that they support certain methods. An interface is never directly created and has no actual representation — other types must be converted to an interface type. An interface defines a contract. A class or structure that implements an interface must adhere to its contract.

The following example shows an interface that contains a default property Item, an event E, a method F, and a property P:

Interface IExample
    Default Property Item(ByVal index As Integer) As String
    Event E()
    Sub F(ByVal value As Integer)
    Property P() As String
End Interface 

Interfaces may employ multiple inheritance. In the following example, the interface IComboBox inherits from both ITextBox and IListBox:

Interface IControl
    Sub Paint()
End Interface 

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

Interface IListBox
    Inherits IControl
    Sub SetItems(ByVal items() As String)
End Interface 

Interface IComboBox
    Inherits ITextBox, IListBox 
End Interface 

Classes and structures can implement multiple interfaces. In the following example, the class EditBox derives from the class Control and implements both IControl and IDataBound:

Interface IDataBound
    Sub Bind(ByVal b As Binder)
End Interface 

Public Class EditBox
    Inherits Control
    Implements IControl, IDataBound

    Public Sub Paint() Implements IControl.Paint
        ' ...
    End Sub

    Public Sub Bind(ByVal b As Binder) Implements IDataBound.Bind
    End Sub
End Class
InterfaceDeclaration ::=
   [ Attributes ] [ InterfaceModifier+ ] Interface Identifier
   [ InterfaceBase+ ]
   [ InterfaceMemberDeclaration+ ]
   End Interface LineTerminator
InterfaceModifier ::= AccessModifier | Shadows

See Also

7.8.1 Interface Inheritance | 7.8.2 Interface Members | | 7.3 Primitive Types | 7.4 Enumerations | 7.6 Structures | 7.5 Classes | 7.7 Standard Modules | 7.9 Arrays | 7.10 Delegates | Interface Statement (Visual Basic Language Reference) | Interfaces Overview (Visual Basic Language Concepts)