by Kevin Wiegand (5 Submissions)
Category: Object Oriented Programming (OOP)
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (14 Votes)
This tutorial is designed to give people an idea of what OOP is (Object Oriented Programming) as it is related to Class Modules and Collections. This uses a very simple example of showing how to create/use Class Properties and Methods. It also shows how to use Collections to create a Collection of Classes.
12.0pt'>Classes/Collections Tutorial by Kevin Wiegand. Please download the Zip file that contains the source code if this tutorial is hard to read due to formatting problems.
12.0pt'>
12.0pt'>Definitions:
12.0pt'> Module
12.0pt'> A term
used to describe where code is stored within Visual Basic.style="mso-spacerun: yes"> The three type of modules are (1) Form
Modules, (2) Standard Modules, and (3) Class Modules.
12.0pt'> Standard Module
12.0pt'> A type of
Module that contains (or should contain) publically accessible code, in other
words, code that is available to any module.
12.0pt'> Class (Module)
12.0pt'> A type of
module that allows you to create objects that contain your customized
properties and methods. (The Standard
Form (Default:Form1) is actually a Class Module!)
12.0pt'> Object
12.0pt'> An Object
is a Control (TextBox, Label), or it can be a Variable that defines an instance
of a Class.
12.0pt'> Object Oriented
Programming (OOP)
12.0pt'> Simply
put, OOP is programming with objects.
12.0pt'> Collection
12.0pt'> A
Collection is simply a group of related Objects.
12.0pt'>
12.0pt'>Why use Class Modules?
12.0pt'> Class modules, as said
before, offer a very useful tool - objects.
Classes can has multiple instances of its code, and each instances
properties/methods belong to that instance only.yes"> Standard Module code can only exist once.
12.0pt'>
12.0pt'>Lets see an example!
12.0pt'> All these definitions,
and a short explanation of Class Modules vs Standard Modules - you need to see
code, right? OK, start Visual Basic,
and start a new Standard Exe Project.
Add a Class Module. Rename
Project1 to ClassTest; Form1 to frmMain; Class1 to clsClassTest.style="mso-spacerun: yes"> Paste the following code into each respective
module, and then save the project:
12.0pt'>
12.0pt'>frmMain:
12.0pt'>***Start Copy***
12.0pt'>'Require Variable Declaration (I believe that VB.Net already requires
that you
12.0pt'>'declare all your variables before use - not only does this save
memory, but
12.0pt'>'it also saves you the hassle of keeping track of things!)
12.0pt'>Option Explicit
12.0pt'>
12.0pt'>'This defines a Collection, it is empty right now, but we will fill it later
:)
12.0pt'>Private fClassCollection As New Collection
12.0pt'>
12.0pt'>'We'll use Form_Keypress instead of using a bunch of CommandButtons -
this is easier
12.0pt'>'to do for this example
12.0pt'>Private Sub Form_KeyPress(KeyAscii As Integer)
12.0pt'> Select Case Chr(KeyAscii)
12.0pt'> Case "a"
12.0pt'> AddItemToCollection
12.0pt'> Case "f"
12.0pt'>
ReturnNamesByFunction
12.0pt'> Case "o"
12.0pt'> PrintNames
12.0pt'> Case "p"
12.0pt'>
ReturnNamesByProperty
12.0pt'> Case "s"
12.0pt'> ReturnNamesBySub
12.0pt'> Case " "
12.0pt'> ClearCollection
12.0pt'> End Select
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub Form_Load()
12.0pt'> 'Set initial Form
properties, if you want, just set the properties in the Properties
12.0pt'> 'Pane, and remove this code
12.0pt'> Me.ScaleMode = vbPixels
12.0pt'> Me.Width = Screen.Width
12.0pt'> Me.Height = Screen.Height
12.0pt'> Me.Move 0, 0
12.0pt'>
12.0pt'> 'Lets us know how many
Objects in our Collection (should be zero right now)
12.0pt'> Me.Caption = "Total
Names in Collection is " & fClassCollection.Count
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub AddItemToCollection()
12.0pt'> 'This is what creates a new
instance of the Object 'ClassTest'
12.0pt'> Dim clsNewClass As New
clsClassTest
12.0pt'> Dim strName As String
12.0pt'>
12.0pt'> 'This sets the Property
'Name' for the ClassTest Object
12.0pt'> clsNewClass.Name =
InputBox("Enter a name:")
12.0pt'>
12.0pt'> 'This adds the newly created
Object to the Collection
12.0pt'> fClassCollection.Add
clsNewClass
12.0pt'>
12.0pt'> 'You need to 'close' the new
Collection Object in order to add another one.
12.0pt'> Set clsNewClass = Nothing
12.0pt'>
12.0pt'> 'Lets us know how many
Objects in our Collection
12.0pt'> Me.Caption = "Total
Names in Collection is " & fClassCollection.Count
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub ClearCollection()
12.0pt'> 'This creates an Object
Variable that will hold references to the Objects in
12.0pt'> 'the Collection
12.0pt'> Dim Obj As Object
12.0pt'>
12.0pt'> For Each Obj In fClassCollection
12.0pt'> 'This uses the LIFO
(Last In First Out) method to remove each Object in the
12.0pt'> 'Collection, putting in
'1' in place of fClassCollection.Count will use the
12.0pt'> 'LILO (Last In Last Out)
method to remove each Object in the Collection.
12.0pt'> fClassCollection.Remove
fClassCollection.Count
12.0pt'> Next Obj
12.0pt'>
12.0pt'> 'Lets us know how many
Objects in our Collection (should be zero right now)
12.0pt'> Me.Caption = "Total
Names in Collection is " & fClassCollection.Count
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub PrintNames()
12.0pt'> 'This creates an Object
Variable that will hold references to the Objects in
12.0pt'> 'the Collection
12.0pt'> Dim Obj As Object
12.0pt'>
12.0pt'> 'Clear the form first
12.0pt'> Me.Cls
12.0pt'> 'Print out a litle message
12.0pt'> Me.Print "The following
" & fClassCollection.Count & " names are in the
Collection:" & vbCrLf
12.0pt'>
12.0pt'> For Each Obj In
fClassCollection
12.0pt'> 'This calls the Method
to Print the Names currently in the Collection
12.0pt'> Obj.PrintName Me
12.0pt'> Next Obj
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub ReturnNamesByFunction()
12.0pt'> 'This creates an Object
Variable that will hold references to the Objects in
12.0pt'> 'the Collection
12.0pt'> Dim Obj As Object
12.0pt'>
12.0pt'> For Each Obj In
fClassCollection
12.0pt'> 'This calls the Method
to Return the Names currently in the Collection, and then
12.0pt'> 'MessageBox it to you,
note that this is actually a function instead of a sub as
12.0pt'> 'from the MsgBoxNames
procedure
12.0pt'> MsgBox Obj.ReturnName,
vbOKOnly + vbInformation
12.0pt'> yes"> Next Obj
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub ReturnNamesByProperty()
12.0pt'> 'This creates an Object
Variable that will hold references to the Objects in
12.0pt'> 'the Collection
12.0pt'> Dim Obj As Object
12.0pt'>
12.0pt'> For Each Obj In
fClassCollection
12.0pt'> 'This calls the Method
to Return the Names currently in the Collection, and then
12.0pt'> 'MessageBox it to you,
note that this is actually a function instead of a sub as
12.0pt'> 'from the MsgBoxNames
procedure
12.0pt'> MsgBox Obj.Name,
vbOKOnly + vbInformation
12.0pt'> Next Obj
12.0pt'>End Sub
12.0pt'>
12.0pt'>Private Sub ReturnNamesBySub()
12.0pt'> 'This creates an Object
Variable that will hold references to the Objects in
12.0pt'> 'the Collection
12.0pt'> Dim Obj As Object
12.0pt'>
12.0pt'> For Each Obj In
fClassCollection
12.0pt'> 'This calls the Method
to MessageBox out the Names currently in the Collection
12.0pt'> Obj.MsgBoxName
12.0pt'> Next Obj
12.0pt'>End Sub
12.0pt'>***End Copy***
12.0pt'>
12.0pt'>clsClassTest:
12.0pt'>***Start Copy***
12.0pt'>Option Explicit
12.0pt'>
12.0pt'>'These are internal variables that any one particular instance of this
Class can see
12.0pt'>'This holds the Name Property
12.0pt'>Private fstrName As String
12.0pt'>'These hold the Max and Min sizes for the GenerateRandomText Function
12.0pt'>Private Const fcMin = 5
12.0pt'>Private Const fcMax = 10
12.0pt'>
12.0pt'>'This sets the Name Property, it is based off of the Private 'fstrName'
Variable
12.0pt'>Property Let Name(ByVal strName As String)
12.0pt'> If strName = ""
Then
12.0pt'> 'This will call the
Private Sub to create a random jumbled string for the
12.0pt'> ''Name' property if the
user add a name that is empty
12.0pt'> Randomize Timer
12.0pt'> GenerateRandomText (Rnd
* (fcMax - fcMin)) + fcMin
12.0pt'> Else
12.0pt'> fstrName = strName
12.0pt'> End If
12.0pt'>End Property
12.0pt'>
12.0pt'>'This returns the Name Property, it also must be based on the Private
'fstrName' Variable
12.0pt'>Property Get Name() As String
12.0pt'> Name = fstrName
12.0pt'>End Property
12.0pt'>
12.0pt'>'This is a simple Private Procedure contained in this Class.style="mso-spacerun: yes"> Any new instance of this class
12.0pt'>'cannot specifically call this Procedure, it can only be called by
itself
12.0pt'>Private Sub GenerateRandomText(ByVal intSize As Integer)
12.0pt'> Dim lngCounter As Long
12.0pt'> Dim strTemp As String
12.0pt'> Dim bytRnd As Byte
12.0pt'>
12.0pt'> Randomize Timer
12.0pt'>
12.0pt'> For lngCounter = 1 To
intSize
12.0pt'> bytRnd = CByte((Rnd *
(Asc("z") - Asc("a"))) + Asc("a"))
12.0pt'> strTemp = strTemp &
Chr(bytRnd)
12.0pt'> Next lngCounter
12.0pt'>
12.0pt'> Name = strTemp
12.0pt'>End Sub
12.0pt'>
12.0pt'>'This is a simple Public Procedure contained in this Class.style="mso-spacerun: yes"> Any new instance of this class
12.0pt'>'can specifically call this Procedure
12.0pt'>Public Sub MsgBoxName()
12.0pt'> MsgBox fstrName, vbOKOnly +
vbInformation
12.0pt'>End Sub
12.0pt'>
12.0pt'>'This is a simple Public Procedure contained in this Class.style="mso-spacerun: yes"> Any new instance of this class
12.0pt'>'can specifically call this Procedure
12.0pt'>Public Sub PrintName(ByVal destObj As Object)
12.0pt'> 'Notice how I called the
Property 'Name' instead of referencing the 'fstrName' Variable
12.0pt'> destObj.Print Name
12.0pt'>End Sub
12.0pt'>
12.0pt'>'This is a simple Public Function contained in this Class.style="mso-spacerun: yes"> Any new instance of this class
12.0pt'>'can specifically call this Function
12.0pt'>Public Function ReturnName() As String
12.0pt'> ReturnName = fstrName
12.0pt'>End Function
12.0pt'>***End Copy***
12.0pt'>
12.0pt'>To use this example you can:
12.0pt'> type 'a' to add a name
to the collection, leave the input box empty to create a random string
12.0pt'> type 'f' to MessageBox
the names in the Collection, called using a Function in the Class
12.0pt'> type 'o' to print the
name in the Collection, called using a Procedure in the Class that contains a
Private Procedure within the Class.
12.0pt'> type 'p' to MessageBox
the names in the Collection, called using the Name Property in the Class
12.0pt'> type 's' to MessageBox
the names in the Collection, called using a Procedure in the Class
12.0pt'> type ' ' (spacebar) to
clear the Collection
12.0pt'>
12.0pt'>Final Notes:
12.0pt'> In this tutorial, you
should have learned how to create and use Class Modules.style="mso-spacerun: yes"> You have learned how to create and use Class
Properties, and you have learned how to create and use Class Methods (Methods
as Public Procedures, Methods as Public Functions, and Methods as Private
Procedures). You have also learned how
to use Collections. You have learned
how to Add Objects to a Collection, and Remove Objects from a Collection, as
well as loop through each Object in a Collection.
12.0pt'> If you find any bugs
or problems with this tutorial, please let me know!yes"> If you have anything to add or comment on, please let me
know! If you have found this tutorial
helpful, please vote! I can be reached
at [email protected], or visit my WebSite at
http://www.geocities.com/wieganka, or my mirror site at http://4.41.60.122