This method of referring to an object's Interface in client code relies on
the fact that you can set a variable whose type is the Interface class to point
to an instantiated object from a class that implements the Interface. After you
have done this, any reference in your code to the Interface object variable actually
refers to the instantiated implementing object.
Continuing the example from the previous sections, recall that the Automobile
class implements the IVehicle interface. If you want to manipulate elements of
the Automobile class in autFord that are provided by the IVehicle interface, you
must declare a second variable of type IVehicle and point that variable to refer
back to autFord.
USING AN OBJECT VARIABLE OF THE INTERFACE TYPE TO GAIN ACCESS TO AN OBJECT'S INTERFACE
'DECLARE AND INSTANTIATE OBJECTS FROM
'THEIR BASE CLASSES
Private autFord As New Automobile
'Declare a variable from the
'appropriate interface class
'to accompany the base-class
'above (but DON'T instantiate
'with As New)
Private vhlFord As IVehicle
Private Sub Form_Load()
'set a reference
'to the variable derived from the
Set vhlFord = autFord
'Following line displays name of
'which is "Automobile" in this case —
'and NOT "IVehicle"
'Write code that manipulates
'the interface by manipulating
'interface object variables.
Private Sub Command1_Click()
MsgBox "Traveled " & _
vhlFord.Travel(0, 0, –1, 1)
Listing 12.19 shows the steps you need to take to use this method to gain access
to the Interface-provided elements of a class object. The steps are as given in
Step by Step 12.4 (step numbers are keyed to the numbers in the example of the
STEP BY STEP
12.4 Using an Object Variable of the Interface Class
Declare and instantiate an object from the Client class. In the example, you
use the Automobile class as described in previous sections, and give the resulting
object instance the name autFord. Note that the example uses the As New keyword
to instantiate the object at the same time that you declare it.
Declare an uninstantiated object that uses the Interface class as its type.
In the example, you declare a second variable (named vhlFord in the example) of
type IVehicle (recall that the Automobile class implements IVehicle). Make sure
that it's uninstantiated. In other words, be sure that you don't use the New keyword
when you declare it. This is because you will later set this variable to point
to the autFord variable.
Use the Set = syntax to set the uninstantiated variable that you derived from
the Interface class (vhlFord in the example) to point to the object that you instantiated
from the Client class (autFord). This means that any reference to the Interface-type
variable (vhlFord) will actually be a reference to the implementation of the Interface
in the instantiated variable (autFord). To illustrate this point, the example
code (see 3A) checks the TypeName of the variable vhlFord. Although you might
expect TypeName to be "IVehicle", it actually turns out to be "Automobile".
Programmatically manipulate the members of the object declared from the Interface
class (vhlFord's members in the example). When you manipulate these members, you
are actually manipulating the previously instantiated object (autFord in the example).
In your client code, you use the As New declaration to instantiate an object
reference to the Automobile class named autFord. Then, you declare (but don't
instantiate with New!) a second variable from the IVehicle interface named vhlFord.
In your code, use the Set statement to cause vhlFord to point to autFord. After
that, any reference to vhlFord actually refers to autFord.