Object Notes


Object Allocation
Object Comparison
Me
Nothing
Deleting an Object

Object Allocation

Objects are not created until it is first referred to outside of the declaration statement.  The overhead of creating the object is not incurred unless the conditions are met that determine that it will be used.

For example,

Dim Emp as new CEmployee   ' object has not yet been created

Emp.Firstname = "Bob"             ' object has been created, the Initialize event was triggered, and 
                                                  ' the Firstname property is set to "Bob". 

Only after storage for the object has been allocated does the Initialize event run.


Object Comparison

The Is operator can be used to determine if two object references refer to the same object, i.e., if the two objects are equivalent.  

' Declare a CEmployee object
Dim Emp1 As New CEmployee

' Declare a reference (pointer) to a CEmployee object
Dim Emp2 as CEmployee

' Set Emp2 to refer to the same object as Emp1
Set Emp2 = Emp1 

If Emp1 Is Emp2 Then
     Call MsgBox ("Same employee object",  vbOKOnly + vbInformation )
End If


Me

The Me implicit variable refers to the currently executing class instance (or object).  Other languages use this to refer to the current class instance.


Nothing

If an object reference (variable) has a value of Nothing, then it does not contain a valid reference to an object.  For example, to see if a variable points to an object,

If Not Emp2 Is Nothing Then
    Set Emp2 = Emp1
End If


Deleting an Object

An object can be marked for deletion by setting it to Nothing:

    Set Emp2 = Nothing

Visual Basic performs automatic garbage collection. Objects are terminated (and the Terminate method run) when their variables go out of scope and there are no references to them.