Search Tools Links Login

Class Module Tutorial


Visual Basic 6, or VB Classic

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


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 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.


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
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 ReplaceChar


Note 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.




About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 106 times

Categories

Visual Basic 6

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.