A Visual Basic .NET interface is neither a Visual Basic .NET class nor a COM class. The Component Object Model (COM) is language-neutral. Although Visual Basic .NET interfaces are also language-neutral, they represent an evolutionary progression from COM and ActiveX. Visual Basic .NET interfaces are managed code and COM is considered unmanaged code, meaning there is not a 1:1 mapping between COM and .NET interfaces. For now, suffice it to say that COM interfaces are not .NET interfaces. COM interop allows COM objects and .NET interfaces to interoperate ; the subject of interoperation is discussed in Chapter 14, ".NET Framework." In this section we will be looking at how to define and implement Visual Basic .NET interfaces and comparing .NET interfaces to .NET classes.
Interfaces Compared to Classes
Interfaces in Visual Basic .NET define an interface only. Although Visual Basic .NET interfaces provide no implementation, classes can provide both an interface and an implementation. Visual Basic .NET classes provide an interface through abstract methods and an implementation through nonabstract methods .
There are only a few basic differences between interface and class grammar, as follows :
To keep the differences between interfaces and classes distinct in your mind, think of a class as describing a thing and an interface as describing a capabilities-adapter for a thing. For example, television is a good concept captured by a class. A cigarette lighter adapter for a television is a reasonable interface. A better interface for a television would be the capabilities of turning the television on and off and changing channels. The controls physically on the outside of the television could implement the control interface, as well as a remote control device implementing the control interface. The television and the remote control are two disparate things, but they both implement the interface necessary to control a television: TurnOn, TurnOff, ChangeChannel, and perhaps a method for selecting a specific channel.
The subsections that follow demonstrate how to define and implement an interface by implementing a television class and a control interface, and implementing the controls on both the television class and a remote control class.
Defining an Interface
The example consists of a Television class, a RemoteControl class, and a Control interface. Both the Television and RemoteControl classes implement the Control interface. The RemoteControl is initialized with a reference to a TV instance, simulating the infrared communication between TV and remote. The visual displays simulating the TV and remote are implemented using the Windows Media Player and Button controls (shown in Figure 10.7).
Figure 10.7. The form representing the television and remote control.
The purpose of this section is to demonstrate interfaces in Visual Basic .NET, so we will emphasize the Control interface in this section.
Listing 10.8 The Control interface listing.
1: Public Interface Control 2: Sub Power() 3: Sub VolumeUp() 4: Sub VolumeDown() 5: Sub ChannelUp() 6: Sub ChannelDown() 7: Sub SelectChannel(ByVal Value As String) 8: Property Channel() As String 9: Sub Display() 10: Event PowerChange(ByVal IsOn As Boolean) 11: End Interface
The Control interface demonstrates that interfaces are definitions without implementations . Every class that implements the Control interface must implement all of the interface members. Notice that there are no access specifiers; every member of an interface is public.
Implementing an Interface
A remote control passes requests from a TV viewer to the television. For our purposes a reasonable implementation is to implement the control interface and send the user -request to the TV (simulating an infrared signal). Listing 10.9 demonstrates the RemoteControl class, which implements the Control interface.
Listing 10.9 The RemoteControl class implements the Control interface.
1: Public Class RemoteControl 2: Implements Control 3: Private FTV As Television 4: 5: Public Event PowerChange(ByVal IsOn As Boolean) _ 6: Implements Control.PowerChange 7: 8: Protected Overridable Sub OnPowerChange() 9: RaiseEvent PowerChange(FTV.IsOn) 10: End Sub 11: 12: Public Sub New(ByVal TV As Television) 13: MyBase.New() 14: FTV = TV 15: End Sub 16: 17: Public Sub ChannelDown() Implements Control.ChannelDown 18: FTV.ChannelDown() 19: End Sub 20: 21: Public Sub ChannelUp() Implements Control.ChannelUp 22: FTV.ChannelUp() 23: End Sub 24: 25: Public Sub VolumeDown() Implements Control.VolumeDown 26: FTV.VolumeDown() 27: End Sub 28: 29: Public Sub VolumeUp() Implements Control.VolumeUp 30: FTV.VolumeUp() 31: End Sub 32: 33: Public Sub SelectChannel(ByVal Value As String) _ 34: Implements Control.SelectChannel 35: FTV.SelectChannel(Value) 36: End Sub 37: 38: Public Property Channel() As String Implements Control.Channel 39: Get 40: Return FTV.Channel 41: End Get 42: Set(ByVal Value As String) 43: FTV.Channel = Value 44: End Set 45: End Property 46: 47: Public Sub Display() Implements Control.Display 48: FTV.Display() 49: End Sub 50: 51: Public Sub Power() Implements Control.Power 52: FTV.Power() 53: OnPowerChange() 54: End Sub 55: End Class
Line 2 indicates that RemoteControl is going to implement all of the members of the Control interface. Line 3 maintains a reference to the television set simulating infrared communication. (We could add an infrared transmitter and receiver if it suited our needs; we do not need to go that far to demonstrate interfaces, though.) If you compare the number of methods that have Implements clauses, you will see that there is a one-to-one relationship between interface members in the Control interface and methods with Implements clauses in the RemoteControl class. You must implement every member of an interface.
Notice that methods that implement an interface are public in Visual Basic .NET. You can call interface methods from consumers in Visual Basic .NET. For example, you could create an instance of the RemoteControl class and call the interface methods directly or access the interface methods via a reference to the interface type.
In VB6, implemented interface methods were private and consumers could not call them directly.
Notice that we can define and implement interfaces with methods, properties, and events. The Control interface contains several methods, the Channel property, and a PowerChange event.
The most significant difference between interface definitions and implementationsrefer to Listings 10.8 and 10.9is that interfaces do not contain implementations of members.
Interfaces support inheritance too. You can define an interface and define a second interface that inherits from the original interface. By adding new interface members to the new interface, you can extend the definition of an interface or inherit from multiple interfaces.
Any class that implements an interface must implement all of the members of the interface. Refer back to Listing 10.3, which demonstrated the syntax for multiple interface inheritance.