VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Method A: Using an Object Variable in the Client

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.


Option Explicit

'STEP 1)
Private autFord As New Automobile

'STEP 2)
   'Declare a variable from the
   'appropriate interface class
   'to accompany the base-class
   'object instantiated
   'above (but DON'T instantiate
   'with As New)
   Private vhlFord As IVehicle

Private Sub Form_Load()

'STEP 3)
  'set a reference
  'to the variable derived from the
  'Interface class
  Set vhlFord = autFord

  'Following line displays name of
  'Base Class,
  'which is "Automobile" in this case —
    'and NOT "IVehicle"
     Debug.Print TypeName(vhlFord)
End Sub

'STEP 4)
  'Write code that manipulates
  'the interface by manipulating
  'the appropriate
  'interface object variables.
Private Sub Command1_Click()
  MsgBox "Traveled " & _
  vhlFord.Travel(0, 0, –1, 1)
End Sub

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 listing).

12.4 Using an Object Variable of the Interface Class

  1. 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.

  2. 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.

  3. 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".

  4. 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.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved