Interface Changes for Visual Basic 6.0 Users

An interface, as defined in Visual Basic, is a type that defines a list of public members. An Interface type cannot be instantiated or implemented.

The interface of a class is its list of public members. The Interface statement, in Visual Basic 2008, allows you to declare a type that defines a list of public members. When a class includes the Implements statement, it adds the members of the interface by implementing each member of the interface. This is in contrast to implementation inheritance using the Inherits keyword. In implementation inheritance, the members are inherited from the base class and do not have to be implemented in the derived class.

In the .NET Framework, interfaces are often used by classes to provide support for some service in the application. As an example, the .NET Framework provides that if a class implements the System..::.IComparable interface, then the runtime will sort instances of the class if they are contained in an ArrayList.

For more information on using interfaces in Visual Basic 2008, see Interfaces in Visual Basic.

Visual Basic 6.0

In Visual Basic 6.0, any class can act as an interface. Any other class can implement the interface of another class, using the Implements Statement, as shown below.

' Contents of class BaseClass
Public Sub BaseMethod()
End Sub

' Contents of class DerivedClass
Implements BaseClass
Private Sub BaseClass_BaseMethod()
End Sub

While individual members of the class do not have to contain any code, they may do so.

Visual Basic 2008

Visual Basic 2008 makes a clear distinction between classes and interfaces. One is declared in a Class statement; the other is declared in an Interface statement. A class cannot act as an interface. To act as an interface, the type must be declared using Interface. Members of an interface are not implemented. In fact, the syntax does not allow such lines of code as End Sub in an interface definition. Members of an interface are implemented in the class that declares an interface using the Implements statement:

Interface Printable
    Sub Print()
    Property Mode() As Integer
End Interface

Public Class Tree
    Implements Printable

    Private modeValue As Integer
    Public Property Mode() As Integer Implements Printable.Mode
            Return modeValue
        End Get
        Set(ByVal Value As Integer)
            modeValue = Value
        End Set
    End Property

    Public Sub Print() Implements Printable.Print
        ' Add code to print a tree.
    End Sub
End Class

The only option for implementing inheritance in Visual Basic 6.0 is by using the Implements statement and a base class. In newer versions of Visual Basic, there are two types of inheritance: implementation inheritance and interface inheritance. The type you should select depends on your application. Issues to consider include:

  • Implementation inheritance, using the Inherits Statement, allows you to create new classes without writing more code. You only add code when you want to modify the base class behavior. With interface inheritance, you have to add code for every base class member.

  • Interfaces can be used when you want to inherit from more than one class. Visual Basic 2008 only supports inheriting from one class, but a class can implement multiple interfaces.

  • The .NET Framework defines several interfaces that simplify common programming tasks. Examples include IComparable, ISerializable, and IFormattable.

  • For many .NET Framework interfaces, the .NET Framework also supplies a class that implements the interface. An example is the Component class that implements the IComponent interface. By inheriting from Component using the Inherits Statement, a class gains all the features of the IComponent interface without having to write any code.

Community Additions