Class Module Tutorial For Beginners
Class Module Tutorial For Beginners
This project is designed to be tutorial for implementing a class module. I wrote this in
order to learn more about modules. I used character replacement as the task since it
I'll start by giving a brief explanation of what a class module is. When you create a class module, you are basically creating an This object has properties, methods, and events like the controls For example, Caption is a property of a label, Clear
object.
This class module allows the user to replace a chosen character with another character in
a given string.
It should take the user between 30 minutes and 45 minutes to complete this project.
Steps.
1.
2.
3.
Label1 Label2 Label3 txtString txtChar txtReplacement Frame1 Label4 Label5 lblResult lblCount cmdReplace cmdClear cmdExit
Caption Caption Caption Text Text Maxlength Text Maxlength Caption Caption Caption Caption BorderStyle Caption BorderStyle Caption Caption Caption
Enter String Enter Character Enter Replacement "" "" 1 "" 1 Out Come Result Number of Replacements "" 1-Fixed Single "" 1-FixedSingle Replace Clear Exit
4.
Right Click on Project1 in the Project window. Select Class Module. Select Class Module
5.
Right Click on the Class Module in the Project Change the name property to ReplaceChar. This will be the
Window.
6.
Option Explicit
Notice that the variables are private and the the event is public. The variables actually hold values for the properties. Only
Since they are private, the program itself cannot manipulate them. the module can change them. character in length. Two of the strings are limited to 1
7.
Type the name of the property (ToBeReplaced) and select property option.
should be public for this property. Click OK. This will create two subs. One to
send data to the main project (Get) and one to receive data (Let).
You
will have to change the parameters to the variable types listed below.
8.
The ToBeReplaced property hold the value of the character that will be replaced.
ToBeReplaced = mToBeReplaced
End Property
Get is used to send information from the object to the program. program is getting information. Notice, the properties equal the
The
mToBeReplaced = strChoice
End Property
The
9.
Repeat the above the process for the ReplaceWith The ReplaceWith property holds the value to replace the desired
Property.
character with.
ReplaceWith = mReplaceWith
End Property
mReplaceWith = strChoice
End Property
10.
Count = mCount
End Property
11.
Now, we are going to add a method to the class Methods can consist of funtions or procedures. This method
module.
scans the string and makes the replacements. event. Look toward the bottom of the code.
mCount = 0
'#######################################
'#######################################
intLoop = 1
strTemp = ""
strHold = strString
intLen = Len(strString) + 1
intLoop = intLoop + 1
strTest = Left(strHold, 1)
mCount = mCount + 1
Else
End If
Loop
'#######################################
'#######################################
ReplaceChar = strTemp
Else
RaiseEvent NoSubstitute(strTemp)
End If
End Function
12.
Provide everything was entered correctly, the class Save it and go back to the form.
13.
This declares a
Option Explicit
Note that WithEvents is not required. necessary if you want to use events.
However, it is
14.
Enter the code for the cmdReplace_Click Event. Next, set the
You have to create a new instance of the object first. properties ToBeReplaced and ReplaceWith. method.
'was created.
ReplacementString.ToBeReplaced = txtChar.Text
ReplacementString.ReplaceWith = txtReplacement.Text
lblResult.Caption = ReplacementString.ReplaceChar(txtString.Text)
lblCount.Caption = ReplacementString.Count
End Sub
15.
Program the event procedure for the class The event fires if no replacements were made. You can code I used a
module.
whatever actions want to transpire when the event happens. message box to alert the user that no changes were made.
MsgBox "No substitutions were made in " & strString, vbOKOnly, "Warning"
End Sub
16.
lblResult.Caption = ""
lblCount.Caption = ""
txtChar.Text = ""
txtReplacement.Text = ""
txtString.Text = ""
txtString.SetFocus
End Sub
End
End Sub
17.
That's it.
module can be inserted in other programs now. with text box or labels.
RepStr.ReplaceWith = "_"
strString = RepStr.ReplaceChar(strString)
if RepStr.Count = 0 then
End if
This would replace all space in a string with an underscore. Pretty useful.