Event Procedures in the Class Module

An event is basically an action recognized by an object. Custom classes recognize only two events: Initialize and Terminate. These events are triggered when an instance of the class is created and destroyed, respectively. The Initialize event is generated when an object is created from a class (see the preceding section on creating an instance of a class). Because the statements included inside the Initialize event are the first ones to be executed for the object, before any properties are set or any methods are executed, the Initialize event is a good place to perform initialization of the objects created from the class. The Class_Initialize procedure uses the following syntax:

Private Sub Class_Initialize()

[code to perform tasks as the object is created goes here] End Sub

The Terminate event occurs when all references to an object have been released. This is a good place to perform any necessary cleanup tasks. The Class_Terminate procedure uses the following syntax:

Private Sub Class_Terminate() [cleanup code goes here] End Sub

To release an object variable from an object, use the following syntax:

Set objectVariable = Nothing

When you set the object variable to Nothing, the Terminate event is generated. Any code in this event is executed then.

0 0

Post a comment