Saturday, December 26, 2009

Revisiting Object Keywords

Armed with all the intimate knowledge about objects that I’ve now given you, you should find it very simple to understand the real mechanism behind a few VBA keywords.
The New keyword
The New keyword (when used in a Set command) tells Visual Basic to create a brand-new instance of a given class. The keyword then returns the address of the instance data area just allocated.
The Set command
The Set command simply copies what it finds to the right of the equal sign into the object variable that appears to the left of it. This value can be, for example, the result of a New keyword, the contents of another variable that already exists, or the result of an expression that evaluates to an object. The only other tasks that the Set command performs are incrementing the reference counter of the corresponding instance data area and decrementing the reference counter of the object originally pointed to by the left-hand variable (if the variable didn’t contain the Nothing value):
Set P1 = New CPerson ‘ Creates an object, stores its address
Set P2 = P1 ‘ Just copies addresses
Set P2 = New CPerson() ‘ Lets P2 point to a new object, but also
‘ decrements the reference counter
‘ of the original object
The Nothing value
The Nothing keyword is the Visual Basic way of saying Null or 0 to an object variable. The statement
Set P1 = Nothing
isn’t a special case in the Set scenario because it simply decreases the reference counter of the instance data block pointed to by P1 and then stores 0 in the P1 variable itself, thus disconnecting it from the object instance. If P1 was the only variable currently pointing to that instance, Visual Basic also releases the instance.
The Is operator
The Is operator is used by Visual Basic to check whether two object variables are pointing to the same instance data block. At a lower level, Visual Basic does nothing but compare the actual addresses contained in the two operands and return True if they match. The only possible variant is when you use the Is Nothing test, in which case Visual Basic compares the contents of a variable with the value 0. You need this special operator because the standard equal symbol, which has a completely different meaning, would fire the evaluation of the objects’ default properties:
‘ This code assumes that P1 and P2 are CPerson variables, and that
‘ Name is the default property of the CPerson class.
If P1 Is P2 Then Print "P1 and P2 point to the same CPerson object"
If P1 = P2 Then Print "P1’s Name and P2’s Name are the same"
The TypeOf ... Is statement
You can test the type of an object variable using the TypeOf...Is statement:
If TypeOf P1 Is CPerson Then
Print "P1 is of type CPerson"
ElseIf TypeOf P1 Is CEmployee Then
Print "P1 is of type CEmployee"
End If
You should be aware of a couple of limitations. First, you can test only one class at a time, and you can’t even directly test to see whether an object is not of a particular class. In this case, you need a workaround:
If TypeOf dict Is Scripting.Dictionary Then
‘ Do nothing in this case.
Else
Print "DICT is NOT of a Dictionary object"
End If
Second, the preceding code works only if the Scripting library (or more in general, the referenced library) is currently included in the References dialog box. If it isn’t, Visual Basic will refuse to compile this code. This is sometimes a nuisance when you want to write reusable routines.
Tip You often use the TypeOf ...Is statement to avoid errors when assigning object variables, as in this code:
‘ OBJ holds a reference to a control.
Dim lst As ListBox, cbo As ComboBox
If TypeOf obj Is ListBox Then
Set lst = obj
ElseIf TypeOf Obj Is ComboBox Then
Set cbo = obj
End If
But here’s a faster and more concise way:
Dim lst As ListBox, cbo As ComboBox
On Error Resume Next
Set lst = obj ‘ The assignment that fails will leave
Set cbo = obj ‘ the corresponding variable set to Nothing.
On Error Goto 0 ‘ Cancel error trapping.

No comments:

Post a Comment