Class Module Tutorial
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.
Original Author: Jerry Barnes
Code
Class Module Tutorial for Beginners This project is designed to be tutorial for implementing a class module. I wrote this in I'll start by giving a brief explanation of what a class module It should take the user between 30 minutes and 45 minutes to 1. Open Visual Basic and select a standard EXE project. 2. Rename the form frmMain and save the project to 3. Add the following controls to the form. The form should be similar to this when you are finished. 4. Right Click on Project1 in the Project window. 5. Right Click on the Class Module in the Project 6. Declare the following variables and events. Option Explicit Notice that the variables are private and the the event is 7. Go to the Tool menu and select Add Procedure. 8. Enter the following code for the two properties. Public Property Get ToBeReplaced() As String Public Property Let ToBeReplaced(ByVal strChoice As String) Let is used to retrieve value from the program. The 9. Repeat the above the process for the ReplaceWith Public Property Get ReplaceWith() As String 10. Finally, add the Count Property. It will be read Public Property Get Count() As Integer 11.� Now, we are going to add a method to the class Public Function ReplaceChar(strString As String) As String 12.� Provide everything was entered correctly, the class 13.� Enter the following declaration.� This declares a Option Explicit Note that WithEvents is not required.� However, it is 14.� Enter the code for the cmdReplace_Click Event.� Private Sub cmdReplace_Click() 15. Program the event procedure for the class Private Sub Replacementstring_NoSubstitute(strString As String) 16. Enter code for the final two command buttons. Private Sub cmdClear_Click() 17. That's it. The program should run. The 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  If you have any suggestions, please feel free to contact me at
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. Â
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.
complete this project.
Steps.
whatever name you like.
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
Select Add from the menu. Select Class Module. Select Class Module
again.
Window. Change the name property to ReplaceChar. This will be the
name of the object.
Private mToBeReplaced As String * 1
Private mReplaceWith As String * 1
Private mCount As Integer
Public Event NoSubstitute(strString As String)
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.
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.
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. The
program is getting information. Notice, the properties equal the
variable declared in the declartions section.
   mToBeReplaced = strChoice
End Property                 Â
program lets the module have information.Â
Property. The ReplaceWith property holds the value to replace the desired
character with.
   ReplaceWith = mReplaceWith
End Property
Public Property Let ReplaceWith(ByVal strChoice As String)
   mReplaceWith = strChoice
End Property
only so it does not have a let property. The count property will return to
the program the number of substitutions made.
Â?Â?Â? Count = mCount
End Property
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.
Â?Â?Â? 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
module is fully functional now.Â? Save it and go back to the form.
variable as a type of the created object.
Dim WithEvents ReplacementString As ReplaceChar
necessary if you want to use events.
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.
Â?Â?Â? 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
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.
'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
   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
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.
underscore. Pretty useful.
jerry_m_barnes@hotmail.com.
About this post
Posted: 2002-06-01
By: ArchiveBot
Viewed: 96 times
Categories
Attachments
CODE_UPLOAD78647172000.zip
Posted: 9/3/2020 3:45:00 PM
Size: 9,698 bytes
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.