The Initialize Event

Let's begin by examining precisely when the Initialize event is fired, then look at some possible applications for the Initialize event handler.

When is the Initialize event fired?

The firing of the Initialize event depends on how the class object is instantiated. If you use the combined method of declaring a New instance of an object:

Dim oVar As New svrObject the Initialize event is fired when the first reference is made to a member of the class, and not when the class is declared as New. For example:

Dim oVar As New svrObject ' Initialize event not called oVar.AnyProp = sAnyVal ' Initialize event fired _

immediately prior to the Property Let

However, if you use the Set statement to instantiate an object, the Initialize event is fired when the Set statement is executed. For example:

Dim oVar As svrObject

Set oVar = New svrObject ' Initialize event fired here

Using the Initialize event

The Initialize event can be used for any of the following:

• To create new collection objects that are used within the class. For example: Set mcolx = New Collection

• To include conditional debugging code to determine when the class has been initialized. For example:

#If ccDebug Then

Debug.Print "xyz Class Initialized" #End If

• To create and instantiate dependent objects. For example: Set moDepObj = New clsDependant

64 Chapter 4 - Class Modules

Was this article helpful?

0 0

Post a comment