VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

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

Implementing Custom Events in Class Modules

You can define and fire your own custom-defined events in a class module. The act of firing an event from within the class module's code is known as raising the event. When an object instantiated from your class raises an event, the controller code can handle that event in an event procedure.

Declaring the Event

You must first define the event by declaring it in the General Declarations section of the class. The syntax for an event declaration is Public Event EventName (argument list) where argument list is a list of arguments declared by name and type similar to the parameter list you would declare in a regular procedure.

You might declare an event named FileFound, for example, with the following line in a class's General Declarations section:

Public Event FileFound _
(FileName as String, _
FilePath As String)

The controlling code would then be able to write an event procedure that received two String-type parameters. As the following section shows, it is the responsibility of the code in the class that raises the event to determine which values the arguments will hold.

Unless you specify otherwise with the ByVal keyword, the arguments are passed by reference. This means that the event procedure in the controller code could change the value of the parameters. Modifiable arguments provide a way for an object and its controller to communicate with each other during processing, because you could specify arguments whose purpose is to carry a message back from the controller.

In the following example, a third Boolean parameter is added: Cancel. The controller might change this to tell our class object to stop processing:

Public Event FileFound _
(FileName as String, _
FilePath As String, _
Cancel as Boolean)

Raising the Event and Implementing Callbacks in a Class Object

The only other thing you need to do in the class to implement an event is to fire, or raise, the event in a single line of code, typically in a method of the class, as shown in Listing 12.5 with the RaiseEvent keyword.

Although, as just stated, you raise the event with a single line of code, you will probably need to write several more lines to fully implement the firing of the event, especially if the event provides arguments to the controller: Before firing the event, you will want to prepare the value of the arguments to be passed to the controller, and after the event fires, you will want to check the arguments' values to see whether the controller has changed any of them.

Notice that the code checks the value of the Cancel argument in Listing 12.5 after the event fires to see whether the controller wants this code in our class to continue processing. Because controllers can modify an event's arguments, this effectively implements callback functionality between controller and object.


'. . .code to assign values of strName and strDir
'initialize value for
'Cancel argument
blnCancel = False
'fire the event
RaiseEvent FileFound(strName, strDir, blnCancel)
'check to see whether controller
'changed the Cancel argument
If blnCancel Then Exit Sub


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved