Microsoft Office Tutorials and References
In Depth Information
Special Events for Classes
VBA defines two special events for all classes, the Initialize event and the Te rminate event.
The Class_Initialize event contains code that will be run when an object is created based on
this class. This event is useful for initializing class-level variables including executing any
Set New statements needed to create any objects that are needed by this object.
Set ObjectVar = New MyClass
The Class_Terminate event contains code that will be run just before an object is destroyed.
This event is an ideal place to destroy any objects that are local to the class by setting them to
Nothing using code like this:
Set ObjectVar = Nothing
Note The Class_Initialize and Class_Terminate events are fired only when the actual
object is created or destroyed. Merely setting one object variable to another will not trigger
the Class_Initialize event. If two or more object variables point to the same object, merely
setting one object variable to Nothing will not trigger the Class_Terminate event.
Resolving References
Sometimes you’ll find yourself in a situation where you have a local variable and a class-level
variable with the same name. This frequently happens when you want to give a parameter in
a method the same name as a property. To differentiate between a class-level variable and a
local variable or parameter, you can prefix the class level variable with Me. as in the following
example:
If Me.Name <> Name Then
In this statement, the variable Me.Name refers to a class-level variable, whereas the unquali­
fied variable Name refers to a local variable or parameter.
Tip Identifying Things That Belong to Me
The keyword Me can also be used to qualify any public or private element of a class from
the code within that class, including class-level variables, subroutines, functions, and prop­
erty routines.
Search JabSto ::




Custom Search