README.md
December 5, 2022 · View on GitHub
Description
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. I hope that it will be of assistance to others.
This program allows the user to replace a chosen character with another character in
a given string.
More Info
| Submitted On | 2000-07-10 14:34:26 |
| By | Jerry Barnes |
| Level | Beginner |
| User Rating | 4.4 (53 globes from 12 users) |
| Compatibility | VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) |
| Category | Miscellaneous |
| World | Visual Basic |
| Archive File | CODE_UPLOAD78647172000.zip |
Source Code
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. I hope that it will be of assistance to others.
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 object. This object has properties, methods, and events like the controls that you put on form. For example, Caption is a property of a label, Clear 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 | Caption | Enter String |
| Label2 | Caption | Enter Character |
| Label3 | Caption | Enter Replacement |
| txtString | Text | "" |
| txtChar | Text | "" |
| Maxlength | 1 | |
| txtReplacement | Text | "" |
| Maxlength | 1 | |
| Frame1 | Caption | Out Come |
| Label4 | Caption | Result |
| Label5 | Caption | Number of Replacements |
| lblResult | Caption | "" |
| BorderStyle | 1-Fixed Single | |
| lblCount | Caption | "" |
| BorderStyle | 1-FixedSingle | |
| cmdReplace | Caption | Replace |
| cmdClear | Caption | Clear |
| cmdExit | Caption | Exit |

The form should be similar to this when you are finished.
4. Right Click on Project1 in the Project window. Select Add from the menu. Select Class Module. Select Class Module again.
5. Right Click on the Class Module in the Project Window. Change the name property to ReplaceChar. This will be the 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. Since they are private, the program itself cannot manipulate them. Only the module can change them. Two of the strings are limited to 1 character in length.
7. Go to the Tool menu and select Add Procedure. Type the name of the property (ToBeReplaced) and select property option. The scope 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 PropertyGet is used to send information from the object to the program. The program is getting information. Notice, the properties equal the variable declared in the declartions section.
Public Property Let ToBeReplaced(ByVal strChoice As String)
mToBeReplaced = strChoice
End PropertyLet is used to retrieve value from the program. The program lets the module have information.
9. Repeat the above the process for the ReplaceWith Property. The ReplaceWith property holds the value to replace the desired 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 only so it does not have a let property. The count property will return to 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 module. Methods can consist of funtions or procedures. This method scans the string and makes the replacements. It also raises an event. Look toward the bottom of the code. If no replacements are made, an event is raised. This will be used in the form's code. Enter the following 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 module is fully functional now. Save it and go back to the form.
13. Enter the following declaration. This declares a variable as a type of the created object.
Option Explicit
Dim WithEvents ReplacementString As ReplaceCharNote that WithEvents is not required. However, it is necessary if you want to use events.
14. Enter the code for the cmdReplace_Click Event. You have to create a new instance of the object first. Next, set the properties ToBeReplaced and ReplaceWith. Next call the ReplaceChar method. 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 module. The event fires if no replacements were made. You can code whatever actions want to transpire when the event happens. I used a 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 module can be inserted in other programs now. It does not have to be used with text box or labels. It can be used purely in code. For example.
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.
If you have any suggestions, please feel free to contact me at jerry_m_barnes@hotmail.com.