Although Object variables are in many ways no different from other types of variables, the fact that they are references to other software components rather than simple values warrants special attention. While objects, classes, and binding are discussed in greater depth in Chapter 4, a short introduction to the subject is nevertheless worthwhile.
Object variables are declared in much the same way as other variables. There are three ways to declare an object variable:
Dim myObject As LibName.ClassName Dim myObject As New LibName.ClassName Dim myObject As Object
In each of the methods shown above, a Private or Public statement can replace the Dim statement, and the same scope rules apply as for other variables.
In the first declaration, the object variable is referenced to the class type library, but no instance of the class is assigned to the variable. At this stage, myObject is set to Nothing. To reference the class in this manner, you must have used the References dialog to add a reference to the class to your project. To assign a reference to a real instance of the class, you must use the Set statement prior to using the variable; for example:
Set myObject = LibName.ClassName
This produces an early bound reference to the object.
In the second declaration, a reference to a new instance of the class is assigned to the object variable, which is now ready to use immediately. Again, to reference the class in ...