When you want to use the members of an Interface in another object class, you
need to take two steps:
Use the Implements declaration to refer to the Interface in the implementing
class's General Declarations section. VB will then automatically create code stubs
inside the implementing class for all the methods and property procedures that
the Interface defines.
It's a good idea to put code in the implementing class into every method and
property procedure provided by VB for the Interface. If you don't put at least
one line of code in each procedure, there may be confusion about your intentions.
Listing 12.18 shows the contents of a class that implements the IVehicle Interface
set up in the preceding section.
IMPLEMENTING AN Interface CLASS IN ANOTHER CLASS
Private m_TireInflation As Integer
Private m_Color As Long
Private m_MAXSpeed As Single
Private Property Let IVehicle_Color(RHS As Long)
m_Color = RHS
Private Property Get IVehicle_Color() As Long
IVehicle_Color = m_Color
Private Property Let IVehicle_MaxSpeed(RHS As Single)
m_MAXSpeed = RHS
Private Property Get IVehicle_MaxSpeed() As Single
IVehicle_MaxSpeed = m_MAXSpeed
Private Function IVehicle_Travel(xStart As Variant, yStart As
Variant, xEnd As Variant, yEnd As Variant) As Single
IVehicle_Travel = Sqr((xEnd - xStart) ^ 2 + (yEnd - yStart)
Public Property Let TireInflation(NewVal As Single)
m_TireInflation = NewVal
Public Property Get TireInflation() As Single
TireInflation = m_TireInflation
Public Sub SuddenStop()
Notice the Implements keyword that points to the IVehicle Interface at the
beginning of the listing. After you have typed this line into your code, VB makes
IVehicle available as an object in your code window, as Figure 12.23 illustrates.
Note in the illustration that all the member procedures (that is, the methods
and the property procedures) appear in the Procedures window when you choose the
IVehicle object from the left-hand drop-down list in the code window. You then
place code in each of the procedures (methods and Property Get/Let/Set procedures)
that the Interface provides in your class, as you see in Listing 12.18.
Because this class is the place where you actually specify how the Interface
elements will behave, you will note the use of Private variables to hold intermediate
values of properties. Note that the name of the parameter passed to Property Let
procedures is RHS. VB automatically supplies this name, which stands for "right-hand
side" (that is, the right-hand side of an assignment statement, such as the b
in "a = b").
Finally, note that this class, named "Automobile", has some members of its
own: the SuddenStop method and the TireInflation property.
Notice in the listing that the members that this example implements through
the Interface are all declared as Private. This is because you want any client
application that uses your class to go through the Interface definition to reach
this class's implementation of the Interface members.
After you have implemented the Interface in another class and implemented that
class's own specific methods, properties, and events, you can use the implementing
class and its accompanying Interfaces in client code, as discussed in the following