Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Built-In Events of Class Modules

Every Class module comes with two predefined events: Initialize and Terminate.

You can find the event procedure stubs for these two events in the class module's code window by clicking the Objects drop-down list (left-hand list box at the top of the code window) and choosing the Class object.

The Procedures drop-down list (right-hand list box at the top of the code window) then displays the Initialize and Terminate event procedure stubs, which you may navigate to with the mouse (see Figure 12.4).

Navigating to a class module's Initialize and Terminate event procedures.
FIGURE 12.4 Navigating to a class module's Initialize and Terminate event procedures.

The Initialize Event

The Initialize event happens whenever a routine in another part of the application or in an ActiveX client uses your class module to instantiate a new copy of an object.

You therefore want to put code in the Initialize event procedure that is appropriate for—well—initialization.

You might set default initial values of properties from the Windows Registry or Private constants, for instance. If your class accesses data, you might open database files here. The example in Listing 12.6 initializes a classwide Private variable that is used as the storage for a pair of Property Let/Property Get procedures.

LISTING 12.6
INITIALIZING A PROPERTY'S VALUE IN THE CLASS MODULE'S INITIALIZE EVENT PROCEDURE

   Private Sub Class_Initialize()
      iNumberOfAccesses = 0
   End Sub

You should remember that the Initialize event takes place before your object is completely instantiated. Therefore, it is not a good place to do things such as instantiating another object variable of the same class: This would cause an infinite recursive loop, as successive copies of the object would try to instantiate each other.

The Terminate Event

The Terminate event happens whenever a routine in another part of the application or in an ActiveX client destroys the object instance of your class module. An object gets destroyed in one of two ways:

  • The variable that was used to hold your class module's object goes out of scope in the calling code.

  • The calling code explicitly destroys the object with the syntax

       Set Object = Nothing.

You might use the Terminate event procedure to close data files or write information to the Windows Registry or INI files, or just to give a farewell message to your user.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved