Professional Documents
Culture Documents
Object
Property
Setting
Class module
Name
Thing
Command1
Caption
Command2
Caption
Command3
Caption
Command4
Caption
Temporary Thing
NoteIf you added the property procedure using the Add Procedure dialog box, on the Tools menu, be sure to delete the
Property Let declaration that is automatically added by this dialog. Property Let is only required for readwrite properties, as
explained in "Putting Property Procedures to Work for You."
The Thing object has one method, ReverseName, which simply reverses the order of the letters in the Name property. It
doesn't return a value, so it's implemented as a Sub procedure. Add the following Sub procedure to the class module.
PublicSubReverseName()
DimintCtAsInteger
DimstrNewAsString
ForintCt=1ToLen(Name)
strNew=Mid$(Name,intCt,1)&strNew
Next
Name=strNew
EndSub
Class modules have two events, Initialize and Terminate. In the Object drop down of the class module, select Class. The
Procedure drop down will show the events. Place the following code in the event procedures:
PrivateSubClass_Initialize()
'Setdate/timeofobjectcreation,tobereturned
'bythereadonlyCreatedproperty.
mdtmCreated=Now
'Displayobjectproperties.
MsgBox"Name:"&Name&vbCrLf&"Created:"_
&Created,,"ThingInitialize"
EndSub
PrivateSubClass_Terminate()
'Displayobjectproperties.
MsgBox"Name:"&Name&vbCrLf&"Created:"_
&Created,,"ThingTerminate"
EndSub
Usually, the Initialize event procedure contains any code that needs to be executed at the moment the object is created, such
as providing the time stamp for the Created property. The Terminate event contains any code you need to execute in order
to clean up after the object when it is being destroyed.
In this example, the two events are being used primarily to give you a visual indication that a Thing object is being created or
destroyed.
&"Created:"&mth.Created,,"FormThing"
EndSub
'Button"ReversetheThing'sName"
PrivateSubCommand2_Click()
mth.ReverseName
'Click"ShowtheThing"
Command1.Value=True
EndSub
'Button"CreateNewThing"
PrivateSubCommand3_Click()
Setmth=NewThing
mth.Name=InputBox(_
"EnteranameforthenewThing")
EndSub
'Button"TemporaryThing".
PrivateSubCommand4_Click()
DimthTempAsNewThing
thTemp.Name=InputBox(_
"EnteranamefortheTemporaryThing")
EndSub
Temporary Thing
The fourth button demonstrates another aspect of object lifetime. When you press it, you'll be prompted for a name for the
temporary Thing.
But wait there isn't a temporary Thing object yet. You haven't seen its Initialize message box. How can you assign it a
name?
Because the variable thTemp was declared As New, a Thing object will be created the moment one of its properties or
methods is invoked. This will happen when the return value of the InputBox is assigned to the Name property. Type a name
and click OK on the InputBox.
You'll now see the Thing Initialize message box, which shows you that the Name property is still blank. When you click OK to
dismiss the message box, the value from the InputBox statement is finally assigned to the Name property. That's a lot of
activity for one line of code.
Of course, as soon as you've done that, the Click event procedure ends, and the variable thTemp goes out of scope. The
object reference for the temporary Thing is released, so you'll see the Thing Terminate message box. Notice that it contains
the name you supplied.
Each time you click this button, another temporary Thing will be created, named, and destroyed.