Modules are used in VB mostly as a code repository where generic
code is stored that is accessible application-wide. Modules are NOT objects;
they’re nothing more than a place to store code.
A class module on the
other hand IS an object and this is probably their
biggest difference from standard modules. The best explanation I
can think of for a class module for those who have never worked
with them before is a cross between a form without a UI (User
interface) and a UDT on steroids.
If you’ve never used classes before then you’re
probably a little confused as to why exactly you should use them
when you could just use UDT’s or standard Modules in their
place however hopefully after reading through this all should
become clear or at least a little less hazy. Rather than trying to
explain why to use a class and how their constructed and used,
I’ll take you through step by step of building a simple
number class, and don’t worry, as long as you’ve done a
bit of programming before then it’s very unlikely
you’re going to find anything new in any code you’ll
find here, the point here is the principle not the code.
If you’ve not already fire up VB, start a new project and
add a new class module, from the top tool bar Insert -> Class
Module. You should now be looking at the blank code window of
the class. Nothing difficult so far - it looks just like a standard
module, lets see what this thing can do.
The class we will build will be a point deviation
class that will be able to perform some basic mathematic
functions like taking the mean/sum/min/max/range etc. As such
we’ll need an array to hold the points in, so well
that to the class now:
In this case we’re just declaring an empty dynamic
array and we’ll need some way of storing the number
of points we have in the array so declare a variable to keep
the count:
These two variables will be in each instance of the class
module, think of it like a UDT with the array and variable in. This
is where things get different though, these variables are
‘Private’ to the class instance and anything outside it
can’t view the variables without us specifically telling the
outside world about their existence. A UDT is completely public and
this is one of the nice things about the class, you can expose only
as much of the internal workings as are needed, all the complexity
is dealt with internally which makes writing complex object driven
applications far easier and intuitive.
In this case however we do want the number of numbers stored
within the class to be exposed as a property of the class like an
element of the UDT, but here’s another cool thing about
classes – we can make the property read only so the value can
be read but not changed directly, this will be handled elsewhere in
the class.
To expose the property we create a little function known as a
“Property Get” statement, which is nothing more than a
simple function just with a fancy name.
- Public Property Get Count() As Long
Count =
ArraySize ' Return the number count
End Property
The “Get” part means that the property can be read,
if we wanted to make the property editable then we’d need to
put in a “Property Let” statement which I’ll show
you here for reference only, DON'T add it to the
class:
- Public Property Let Count(ByVal iNumCount As Long)
ArraySize =
iNumCount ' Set the number count
End Property
Now we’ll add the code to add a point to the array,
I’ll not bother explaining the code as such as it’s
just basic VB, have a look in help for anything you may not have
come across before.
- Public Sub AddPoint(ByVal mypoint As Point) ' Add a
point
ReDim Preserve PointsList(ArraySize)
Set PointsList(ArraySize) = mypoint
ArraySize = ArraySize + 1
End Sub
This will re-declare the array, add our number into it and
increment the counter. Before we give it a try, we’ll add a
function to remove all the numbers as well:
ReDim
MyPoints(0)
MyArraySize = 0
End Sub
COMPLETED CLASS
MODULE CODE
- Dim PointsList()
- Dim ArraySize As Long
- Public Property Get Count() As Long
Count =
ArraySize ' Return the number count
End Property
- Public Sub AddPoint(ByVal mypoint As Point) ' Add a
point
ReDim Preserve PointsList(ArraySize)
Set PointsList(ArraySize) = mypoint
ArraySize = ArraySize + 1
End Sub
ReDim
MyPoints(0)
MyArraySize = 0
End Sub
Finally name the class “PointDeviation” and then
we’re ready to give it a spin. In the next section well
test the initial class.