Use the Initialize event of a class defined with the
Class...End Class construct to prepare the
object or class for use, setting any references to subobjects or
assigning default values to properties and values to class-level
The Initialize event is triggered automatically when a
class is first instantiated by the
Set statement. For example, in the
following code, the
statement generates the Initialize event:
Dim MyObject As MyClass 'some code ... 'initialize event called here Set MyObject = New MyClass StrName = MyObject.CustName
The Initialize event doesn’t take any arguments.
It is best to declare the Initialize event as Private, although this is not required.
While it’s possible to explicitly call the Initialize event from within the object at any stage after the object has been created, it isn’t recommended, because the code in the Initialize event should be written to be “run once” code.
Use the Initialize event of a class module to generate references to dependent objects. For example:
Option Explicit Dim custOrder Set custOrder = New Order ' ...other code Class Order Private cust, itemsOrdered Private Sub Class_Initialize() Set cust = New Customer Set itemsOrdered = New Items End Sub End Class Class Customer ' Implementation of Customer End Class Class Items Dim orderItem(10) Private Sub Class_Initialize() Set orderItem(0) = New Item End Sub ' Other implementation ...