Using The Thing Object

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Option Explicit

Public Name As String


Private mdtmCreated As Date

Property Get Created() As Date


Created = mdtmCreated
End Property

Public Sub ReverseName()


Dim intCt As Integer
Dim strNew As String
For intCt = 1 To Len(Name)
strNew = Mid$(Name, intCt, 1) & strNew
Next
Name = strNew
End Sub

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:

Private Sub Class_Initialize()


' Set date/time of object creation, to be returned
' by the read-only Created property.
mdtmCreated = Now
' Display object properties.
MsgBox "Name: " & Name & vbCrLf & "Created: " _
& Created, , "Thing Initialize"
End Sub

Private Sub Class_Terminate()


' Display object properties.
MsgBox "Name: " & Name & vbCrLf & "Created: " _
& Created, , "Thing Terminate"
End Sub

Using the Thing Object


Add this declaration to the Declarations section of the form module:

Option Explicit
Private mth As Thing

The variable mth will hold a reference to a Thing object, which will be created in the form's Load
event. Put the following code in the Form_Load event procedure, and in the Click event procedures for
the four buttons.
Private Sub Form_Load()
Set mth = New Thing
mth.Name = InputBox("Enter a name for the Thing")
End Sub

' Button "Show the Thing"


Private Sub Command1_Click()
MsgBox "Name: " & mth.Name & vbCrLf _
& "Created: " & mth.Created, , "Form Thing"
End Sub

' Button "Reverse the Thing's Name"


Private Sub Command2_Click()
mth.ReverseName
' Click "Show the Thing"
Command1.Value = True
End Sub

' Button "Create New Thing"


Private Sub Command3_Click()
Set mth = New Thing
mth.Name = InputBox( _
"Enter a name for the new Thing")
End Sub

' Button "Temporary Thing".


Private Sub Command4_Click()
Dim thTemp As New Thing
thTemp.Name = InputBox( _
"Enter a name for the Temporary Thing")
End Sub

You might also like