VB
has, for some time, allowed us to create objects with more than one interface.
This was done using the Implements
keyword. Any time our class implemented a new interface we were required to
write code to implement each method on the interface. While inheritance provides
a preferable alternative to this in many cases, there are still times when we
may want to have our objects implement multiple interfaces.
VB.NET preserves the Implements keyword, and in fact the entire concept of interfaces is enhanced and made simpler as compared to VB6.
Working with Interfaces
VB.NET introduces a formalized structure for declaring interfaces. It also changes the syntax used in classes that implement an interface, making our code much more intuitive and clear.
Interface Declaration
The most visible enhancement is the introduction
of a formal syntax for declaring an interface. This is done by using the
Interface
keyword:
Public
Interface MyInterface
Event MyEvent()
Sub MyMethod()
Function MyFunction(ByVal Param1 As Integer) As Integer
Property MyProperty() As String
End
Interface
This approach is much more formal that
the approaches available in VB6. It is also more powerful – notice that not
only can we declare Sub
,
Function
,
and Property
methods, but also declare events as a formal part of an interface.
All the elements described in this interface must be implemented by any class that chooses to implement this interface.
Overloading Methods
Methods
(Sub
and Function
)
can be declared in an interface using the Overloads
keyword. The rules for overloading are the same as we discussed earlier in the
chapter – each overloaded declaration must have a unique parameter list based
on data type of the parameters.
Declaring an interface with overloaded methods is done as in the following example:
Public
Interface MyInterface
Overloads Sub MyMethod()
Overloads Sub MyMethod(Data As String)
Overloads Function MyFunction(ByVal Param1 As Integer) As Integer
Overloads Function MyFunction(ByVal Param1 As Single) As Integer
End
Interface
When a class uses the Implements
keyword to implement an interface with overloaded methods, the class must implement
each of the overloaded method declarations.
Implementing an Interface
As with VB6,
implementing an interface is done through the use of the
Implements
keyword:
Public
Class TheClass
Implements MyInterface
End
Class
From there, however, things get a bit
different. In VB6 we’d implement the various interface elements as a set of
specially named Private
methods. That approach was unintuitive and was the cause for great confusion
among many developers new to the language. VB.NET addresses this issue by providing
a clear, concise syntax for implementing the interface – again through the application
of the Implements
keyword.
We can simply mark a method in our class as being the implementation of a specific method from the interface:
Public Sub MyMethod()
Implements MyInterface.MyMethod
So, to implement our example interface, we’d have code such as:
Public
Class TheClass
Implements MyInterface
Public Event MyEvent() Implements MyInterface.MyEvent
Public Function MyFunction(ByVal Param1 As Integer) _
As System.Integer Implements OOlib.MyInterface.MyFunction
End Function
Public Sub MyMethod() Implements OOlib.MyInterface.MyMethod
End Sub
Public Property MyProperty() As String _
Implements OOlib.MyInterface.MyProperty
Get
End Get
Set
End Set
End Property
End
Class
As with VB6, when we implement an interface, we must implement all the elements in that interface – including events, methods, and properties.
We can now create client code that interacts with our object via this interface, in addition to the object’s normal interface:
Dim
obj As MyInterface
obj
= New Implementer()
obj.MyMethod
Contrast this to VB6, where the routine to implement a method from an interface was written as follows:
Private
Sub MyInterface_MyMethod()
‘ implementation goes here
End
Sub
The fact that this method implements part
of the interface is only shown by its naming – a pretty obscure thing. We could
declare this as Public
and make it available for external use, but the name of the method couldn’t
change. Now, with VB.NET the name (and scope) of the method is independent of
the interface element being implemented.
Implementing Multiple Interfaces
A
class can have more than one Implements
statement – thus implementing more than one interface. Suppose we have the following
interfaces:
Public
Interface MyInterface
Sub DoSomething()
End
Interface
Public
Interface OtherInterface
Sub DoWork()
End
Interface
We can construct a class that implements both interfaces:
Public Class TheClass
Implements MyInterface
Implements OtherInterface
End Class
Now we have a choice. We can either implement
separate methods to handle DoSomething
and DoWork
:
Private Sub DoSomething()
Implements MyInterface.DoSomething
‘ implementation
goes here
End Sub
Private Overloads Sub
DoWork() Implements OtherInterface.DoWork
‘ implementation
goes here
End Sub
Or, if they do the same thing, we can have a single method implement both methods:
Private Sub DoSomething()
_
Implements
MyInterface.DoSomething, OtherInterface.DoWork
‘ implementation goes here
End Sub
This is done by combining the list of
implemented methods in a comma-separated list following the Implements
keyword.
Comments