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
may be of use after the project is entered. others.
I hope that it will be of assistance to
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.
that you put on form.
is a method of a listbox, and Click is an event for a command button.
This class module allows the user to replace a chosen character with another character in
a given string.
It has properties, a method, and one event.
It should take the user between 30 minutes and 45 minutes to complete this project.
Steps.
1.
Open Visual Basic and select a standard EXE project.
2.
Rename the form frmMain and save the project to
whatever name you like.
3.
Add the following controls to the form.
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
The form should be similar to this when you are finished.
4.
Right Click on Project1 in the Project window. Select Class Module. Select Class Module
Select Add from the menu. again.
5.
Right Click on the Class Module in the Project Change the name property to ReplaceChar. This will be the
Window.
name of the object.
6.
Declare the following variables and events.
Option Explicit
Private mToBeReplaced As String * 1
Private mReplaceWith As String * 1
Private mCount As Integer
Public Event NoSubstitute(strString As String)
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.
Go to the Tool menu and select Add Procedure. The scope
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.
Enter the following code for the two properties.
The ToBeReplaced property hold the value of the character that will be replaced.
Public Property Get ToBeReplaced() As String
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
variable declared in the declartions section.
Public Property Let ToBeReplaced(ByVal strChoice As String)
mToBeReplaced = strChoice
End Property
Let is used to retrieve value from the program.
The
program lets the module have information.
9.
Repeat the above the process for the ReplaceWith The ReplaceWith property holds the value to replace the desired
Property.
character with.
Public Property Get ReplaceWith() As String
ReplaceWith = mReplaceWith
End Property
Public Property Let ReplaceWith(ByVal strChoice As String)
mReplaceWith = strChoice
End Property
10.
Finally, add the Count Property.
It will be read The count property will return to
only so it does not have a let property.
the program the number of substitutions made.
Public Property Get Count() As Integer
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.
It also raises an If no replacements are
made, an event is raised. Enter the following code.
This will be used in the form's code.
Public Function ReplaceChar(strString As String) As String
Dim intLoop As Integer
Dim intLen As Integer
Dim strTemp As String
Dim strTest As String
Dim strHold As String
mCount = 0
'The replacement count should be zero.
'#######################################
'# The following code scans the string #
'# and makes the desired replacements. #
'#######################################
intLoop = 1
strTemp = ""
strHold = strString
intLen = Len(strString) + 1
Do Until intLoop = intLen
intLoop = intLoop + 1
strTest = Left(strHold, 1)
If strTest = mToBeReplaced Then
'mTobeReplaced comes from the properties.
strTemp = strTemp & mReplaceWith
'mReplaceWith comes from the properties.
mCount = mCount + 1
Else
strTemp = strTemp & Left(strHold, 1)
End If
strHold = Right(strHold, Len(strHold) - 1)
Loop
'#######################################
'# Scanning and replacement code ends. #
'#######################################
If mCount <> 0 Then
ReplaceChar = strTemp
'Write the new string.
Else
RaiseEvent NoSubstitute(strTemp)
End If
'If mCount is zero the no replacements
'were made. This means that we want to
'raise the event NoSubstitute.
End Function
12.
Provide everything was entered correctly, the class Save it and go back to the form.
module is fully functional now.
13.
Enter the following declaration.
This declares a
variable as a type of the created object.
Option Explicit
Dim WithEvents ReplacementString As ReplaceChar
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.
Next call the ReplaceChar
Finally use the Count property to get the number of replacements.
Private Sub cmdReplace_Click()
Set ReplacementString = New ReplaceChar
'Create a new object of the class that
'was created.
ReplacementString.ToBeReplaced = txtChar.Text
'Send the property ToBeReplaced. This
'is a Let sub in the module.
ReplacementString.ReplaceWith = txtReplacement.Text
'Send the property ReplaceWith. This
'is a Let sub in the module.
lblResult.Caption = ReplacementString.ReplaceChar(txtString.Text)
'Set the caption of lblResult with the
'results of the Replace method.
lblCount.Caption = ReplacementString.Count
'Get the count through the count property.
'This is a Get sub in the module.
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.
Private Sub Replacementstring_NoSubstitute(strString As String)
'This subs only purpose is to demonstrate using an event. StrString is passed
'from the module back to the program.
MsgBox "No substitutions were made in " & strString, vbOKOnly, "Warning"
End Sub
16.
Enter code for the final two command buttons.
Private Sub cmdClear_Click()
Set ReplacementString = Nothing
'Destroy the object so resources
'are not wasted.
lblResult.Caption = ""
lblCount.Caption = ""
txtChar.Text = ""
txtReplacement.Text = ""
txtString.Text = ""
'Clear the controls.
txtString.SetFocus
'Return to the first text box.
End Sub
Private Sub cmdExit_Click()
Set ReplacementString = Nothing
'Tidy up. Don't waste resources.
End
End Sub
17.
That's it.
The program should run.
The It does not have to be used For example.
module can be inserted in other programs now. with text box or labels.
It can be used purely in code.
Dim WithEvents RepStr As ReplaceChar
Set RepStr = New ReplaceChar
RepStr.ToBeReplace = " "
RepStr.ReplaceWith = "_"
strString = RepStr.ReplaceChar(strString)
if RepStr.Count = 0 then
msgbox "No subs made"
End if
This would replace all space in a string with an underscore. Pretty useful.