This method enables you to pass an object that has been instantiated from the
Client class to a "wrapper" routine. The wrapper routine accepts a parameter whose
type is of the Interface class. Even though its type is of the Interface class,
this parameter provides a reference inside the wrapper routine to the original
object, but only in regard to its features that are implemented in the Interface
class.
LISTING 12.20
USING WRAPPER FUNCTIONS TO GAIN ACCESS TO AN OBJECT'S INTERFACE ELEMENTS
Option Explicit
'STEP 1)
'DECLARE AND INSTANTIATE OBJECTS FROM
'THEIR BASE CLASSES
Private autFord As New Automobile
'STEP 2)
'Create procedures that
'take parameters whose
'type is that of the
'Interface class and manipulate
'that parameter as desired.
Private Function VehicleTravel _
(vhl As IVehicle, x1, y1, x2, y2) As Double
VehicleTravel = vhl.Travel(x1, y1, x2, y2)
'STEP 2A)
'Following line displays name
'of Base Class,
'which is "Automobile,"
'instead of the interface class
'name, "IVehicle"
Debug.Print TypeName(vhl)
End Function
'STEP 3)
'Call the wrapper
'procedures when you need to
'manipulate the base class
'object through its interface.
'This calling code should pass the
'base class object variable
'to the procedures, and NOT
'the variables based on the
'interface class.
Private Sub Command2_Click()
MsgBox "Traveled " & VehicleTravel(vhl747, 0, 0, 3,
4)
End Sub
Listing 12.20 shows the steps you need to take to use this second method to
gain access to the Interface-provided elements of a Class object. The steps are
as given in Step by Step 12.5 (step numbers are keyed to the numbers in the example
of the listing).
STEP BY STEP
12.5 Using Client Wrapper Routines to Refer to an Interface Class Object
-
Declare and instantiate an object from the Client class. This is the same
operation as performed in step 1 of the preceding method, and the same comments
apply. You also use the same Client class as in the example, Automobile, to instantiate
the variable autFord.
-
Create one or more procedures that take an object parameter whose type is
the Interface class. Such a procedure can then manipulate the Interface-derived
elements of this object. In the example, the procedure takes a parameter whose
type is IVehicle (recall that IVehicle is implemented by the Automobile class).
Notice that section 2A in the example checks the actual TypeName of the object
parameter. If you run this code, you will discover that the object's type is not
the type of the Interface class (IVehicle), but rather the type of the object
that was passed from the calling routine (Automobile), as described in the next
step.
-
Call the procedures created in step 2 by passing the instantiated object (autFord
in the example) of the Client class as a parameter. When you pass this object
to the procedure, the Interface manipulation code in the procedure can then access
the Interface-implemented elements of the object.
The use of a wrapper routine typically makes your code cleaner and easier
to maintain.