Unlike normal code modules, class modules support two events that are automatically defined when you add a class module to your project. These two standard events, the Initialize and Terminate events, are analogous to the class constructor and destructor in an object-oriented programming language like C++; they are fired automatically when a class is instantiated and destroyed, respectively.
An event handler is the code attached to a particular event. When the event is fired, the event handler is executed automatically. Like all event handlers, writing code to handle the Initialize and Terminate events is optional, but it's at the heart of sound VB programming. So let's look at some of the uses you can put these event handlers to and some of the rules relating to these two events.
Let's begin by examining precisely when the Initialize event is fired, then look at some possible applications for the Initialize event handler.
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 ...