Initialize Event - Event Procedure

Initialize Event — Event Procedure

         Example

To create the event procedure for the Initialize event, open the class module and click Class in the Object box. Then click Initialize in the Procedure box.

Syntax

Private Sub Class_Initialize( )

Remarks

You can use this event procedure to run code when an instance of the class is created, or to initialize any data used by the instance of the class.

The Initialize event occurs when you create a new instance of a class by using the New keyword. For example, suppose you have a class named CustomObject. You can add code in a standard module that creates a new instance of the class CustomObject, as shown in the following example:

Dim obj As CustomObject
Set obj = New CustomObject

When you run the procedure that contains this code, Visual Basic creates a new instance of the CustomObject class, and the Initialize event occurs.

When declaring an object variable using the New keyword, the Initialize event occurs when you create an instance of a class by setting or returning a property or applying a method defined in the class module. For example, suppose you've defined a Function procedure named ListNames within the CustomObject class module. To run this function from another module, you must qualify it with the name of the class module, as shown in the following example:

CustomObject.ListNames

Qualifying the function with the name of the class module creates an instance of the class module, and the Initialize event occurs.