Object variables not only store information but also perform actions. This
makes them a very special sort of variable, and there are special considerations you should take into account when working with them.
New Objects
Objects are declared slightly differently than other variables. Visual Basic initializes most variables when they are first used. For instance, you declare a new Integer variable with Dim, and then when you first use the variable Visual Basic initializes its value to 0 automatically. Objects aren't initialized this way. If you declare an object variable and then check its type name, you'll get the answer "Nothing":
Dim frmX As Form1 Debug.Print TypeName(frmX) `Displays "Nothing"
You can't do anything with an object variable until you create an instance of it. Use the New keyword to create a new object:
Dim frmX As Form1 Debug.Print TypeName(frmX) `Displays "Nothing" Set frmX = New Form1 Debug.Print TypeName(frmX) `Displays "Form1"
The New keyword is important here. Just dimensioning an object variable creates a variable with that object type, but the variable is set to Nothing until you assign an object to it.
You also can use the New keyword when you declare an object variable, as follows:
Dim frmX As New Form1 Debug.Print TypeName(frmX) `Displays "Form1"
Declaring an object variable with the New keyword tells Visual Basic to create a new object whenever it encounters the object variable in executable code and the object variable is set to Nothing. Since Dim statements aren't executable, Visual Basic waits to create the object till the Debug.Print statement above. This might seem like an unimportant detail, but it can lead to some unexpected results. For instance, the following code does not display "Nothing" as you might expect:
Dim frmX As New Form1 Set frmX = Nothing Debug.Print TypeName(frmX) `Displays "Form1"
The preceding code actually creates an instance of Form1 in the Set statement and immediately destroys it. Then it creates a new instance on the very next line. You can avoid this behavior by not declaring object variables As New. For instance, the following code yields the expected result:
Dim frmX As Form1 Set frmX = New Form1 `Create an instance of the form Debug.Print TypeName(frmX) `Displays "Form1" Set frmX = Nothing `Destroy the form Debug.Print TypeName(frmX) `Displays "Nothing"
In general, you should use
Dim Dear John, How Do I... As New
to create objects that will exist throughout the scope where they are declared and you should use
Dim Dear John, How Do I... As Dear John, How Do I... Set Dear John, How Do I... New Dear John, How Do I...
to create objects which can be created and destroyed within their scope.
Existing Objects
An Object variable only refers to an objectthis isn't the same thing as being the object. You can reassign which object an Object variable refers to by using the Set statement, as shown here:
Dim frmX As New Form1 `Create a new object Dim frmY As New Form1 `Create another object Set frmX = frmY `frmX and frmY both refer to the same object
You must use Set rather than simple assignment to assign an object reference to a variable. Most objects have a default property. Set tells Visual Basic to perform an object assignment rather than a property assignment.
Object Operations
You compare objects using the Is operator. Is tells you whether two Object variables refer to the same object. The following code shows how this works:
Dim frmX As New Form1 `Create a new object Dim frmY As New Form1 `Create another object Debug.Print frmX Is frmY `Displays "False" Set frmX = frmY `frmX and frmY both refer to the same object Debug.Print frmX Is frmY `Displays "True"
How long do objects live? Until they become Nothing!
Objects stick around as long as they have a variable that refers to them or, as is the case with Form objects, as long as they are visible. You can usually control the visibility of an object by using its Visible property. You can get rid of an object by setting its variable to another object or to Nothing:
Dim frmX As New Form1 `Create a new object Dim frmY As Object `Declare a variable of type Object Set frmY = frmX `frmX and frmY both refer to same object Set frmX = Nothing `frmY is still valid; object persists Set frmY = Nothing `Object goes away
At least, that's what's supposed to happen. Some objects aren't so careful. Early versions of some objects (including some from Microsoft) weren't so good about going away. Most objects are better behaved today, but it's still a good idea to be careful when working with objects you can't see. In general, objects you create in Visual Basic are well behaved, and objects in other products are usually, but not always, well behaved.
SEE ALSO
- Chapter 5, "Object-Oriented Programming," for more information about objects