Instantiating and Interacting with Objects


Even though a simple class has been completely specified at this point, no objects have been created yet.  An instance of a class has to be created by declaring an object with the class name as its data type. 

Objects can be declared in two ways:

' Declare a reference to an employee object
Dim Emp as clsEmployee

' Create a new object and save its reference
Set Emp = New clsEmployee

or

' Combined version
Dim Emp as New clsEmployee

The first approach is useful if you want to create multiple references (sometimes called pointers) to the same object.  Without the New keyword, the variable acts as an object reference, but is not itself an object.  In the example below, Emp1 is an object, but Emp2 is merely a pointer to the object referenced by Emp1.

Dim Emp1 as New clsEmployee                             
Dim Emp2 as clsEmployee

set Emp2 = Emp1

 

 

 


Accessor and mutator methods, as well as any other class methods,  are invoked just like normal subs and functions.  Example method calls are highlighted in the figure below.

 


Property methods are not called like normal subs and functions.  An example of calls to the Let and Get methods are shown in the following figure.

 

Property Set and Property Get give the appearance of actually accessing an instance variable directly, but they are still Private and still being accessed through methods.  Some texts make a distinction between instance variables and properties.