Interfaces
An Interface is a way to define a set of behaviors that a class will perform. The definition of an interface is a list of method signatures (name, parameters, and return type). A class having all of the methods is said to “implement” that interface.
In VBA, using interfaces lets the compiler check that a module implements all of its methods. A variable or parameter can be defined in terms of an interface instead of a specific class.
Simple Interface - Flyable
Section titled “Simple Interface - Flyable”The interface Flyable is a class module with the following code:
Public Sub Fly() ' No code.End Sub
Public Function GetAltitude() As Long ' No code.End FunctionA class module, Airplane, uses the Implements keyword to tell the compiler to raise an error unless it has two methods: a Flyable_Fly() sub and a Flyable_GetAltitude() function that returns a Long.
Implements Flyable
Public Sub Flyable_Fly() Debug.Print "Flying With Jet Engines!"End Sub
Public Function Flyable_GetAltitude() As Long Flyable_GetAltitude = 10000End FunctionA second class module, Duck, also implements Flyable:
Implements Flyable
Public Sub Flyable_Fly() Debug.Print "Flying With Wings!"End Sub
Public Function Flyable_GetAltitude() As Long Flyable_GetAltitude = 30End FunctionWe can write a routine that accepts any Flyable value, knowing that it will respond to a command of Fly or GetAltitude:
Public Sub FlyAndCheckAltitude(F As Flyable) F.Fly Debug.Print F.GetAltitudeEnd SubBecause the interface is defined, the IntelliSense popup window will show Fly and GetAltitude for F.
When we run the following code:
Dim MyDuck As New DuckDim MyAirplane As New Airplane
FlyAndCheckAltitude MyDuckFlyAndCheckAltitude MyAirplaneThe output is:
Flying With Wings!30Flying With Jet Engines!10000Note that even though the subroutine is named Flyable_Fly in both Airplane and Duck, it can be called as Fly when the variable or parameter is defined as Flyable. If the variable is defined specifically as a Duck, it would have to be called as Flyable_Fly.
Multiple Interfaces in One Class - Flyable and Swimable
Section titled “Multiple Interfaces in One Class - Flyable and Swimable”Using the Flyable example as a starting point, we can add a second interface, Swimmable, with the following code:
Sub Swim() ' No codeEnd SubThe Duck object can Implement both flying and swimming:
Implements FlyableImplements Swimmable
Public Sub Flyable_Fly() Debug.Print "Flying With Wings!"End Sub
Public Function Flyable_GetAltitude() As Long Flyable_GetAltitude = 30End Function
Public Sub Swimmable_Swim() Debug.Print "Floating on the water"End SubA Fish class can implement Swimmable, too:
Implements Swimmable
Public Sub Swimmable_Swim() Debug.Print "Swimming under the water"End SubNow, we can see that the Duck object can be passed to a Sub as a Flyable on one hand, and a Swimmable on the other:
Sub InterfaceTest()
Dim MyDuck As New DuckDim MyAirplane As New AirplaneDim MyFish As New Fish
Debug.Print "Fly Check..."
FlyAndCheckAltitude MyDuckFlyAndCheckAltitude MyAirplane
Debug.Print "Swim Check..."
TrySwimming MyDuckTrySwimming MyFish
End Sub
Public Sub FlyAndCheckAltitude(F As Flyable) F.Fly Debug.Print F.GetAltitudeEnd Sub
Public Sub TrySwimming(S As Swimmable) S.SwimEnd SubThe output of this code is:
Fly Check… Flying With Wings! 30 Flying With Jet Engines! 10000 Swim Check… Floating on the water Swimming under the water