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
USING WRAPPER FUNCTIONS TO GAIN ACCESS TO AN OBJECT'S INTERFACE ELEMENTS
'DECLARE AND INSTANTIATE OBJECTS FROM
'THEIR BASE CLASSES
Private autFord As New Automobile
'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)
'Following line displays name
'of Base Class,
'which is "Automobile,"
'instead of the interface class
'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
Private Sub Command2_Click()
MsgBox "Traveled " & VehicleTravel(vhl747, 0, 0, 3,
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
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