Touch of Class

Classes have been likened to rubber stamps, cookie-cutters, and a raft of other everyday items in an attempt to make the concept more easily understandable. Because you are reading a book on software development, it seems fairly safe to assume that you understand the concept of a template, such as a Microsoft Word template. That analogy succinctly describes the role of class modules and the distinction between them and class objects.

Just as a class module is equivalent to a Word template, a class object is equivalent to a Word document that is based on that template. Of course, with VBA class modules, you don't define styles or boilerplate text, but you do define a set of properties that includes their data types and read-write attributes. You also define the methods of a class, the data types they return (if any), and the events the class exposes to the calling procedure. It is these properties and methods that constitute the object's interface to the programming environment.

Each unique class object will be exactly the same as the class module it was based on, except of course, for the data it contains. In fact, the class module never gets instantiated and never contains any data because you don't actually work on it. You can, however, create as many instances of it as you like, in the form of class objects, each identified by a different name. To make a change to all the class objects, you need change only the class module. Probably the easiest way to describe a class is to compare it to a standard VBA module.

VBA modules can contain many procedures, such as subs and functions, all of which were explained in Chapter 2.

For instance, you may have a VBA module called modClassroom that contains procedures to implement a single property of a classroom — the number of students in the class:

Option Compare Database Option Explicit

Private mintStudents As Integer

Public Sub AddStudent()

mintStudents = mintStudents + 1 End Sub

Public Function GetStudents() As Integer

GetStudents = mintStudents End Function

Your property, the number of students, is stored in a module-level variable called mintStudents. To add a student to the classroom, you call the AddStudent() procedure, and to retrieve the current count you call the GetStudents() function. The potential problem with this approach is illustrated by Figure 13-1.

Figure 13-1

What if you have another module somewhere that also uses the AddStudent() procedure? It would change the value of mintStudents. To ensure you can change the number of students for different classrooms, you would have to either create multiple AddStudent procedures, or implement some other way of doing it, such as arrays.

This is where class modules come in. Take a look at the following class module called clsClassroom. Don't worry if you don't quite understand it; all will be explained as you go along.

Option Compare Database Option Explicit

Private mintStudents As Integer

Public Sub AddStudent()

mintStudents = mintStudents + End Sub

1

Public Property Get Students() As

Students = mintStudents End Property

Integer

This class is virtually the same as modClassroom. The nifty part about it is the fact that the code used to define the class is essentially a template that you can use to create as many classroom objects as you wish. Further, if you had two different procedures that each called AddStudent(), they would each operate on a different copy, or instance, of the clsClassroom class illustrated by Figure 13-2.

Class 1 Class 2

Class 1 Class 2

For example, the following VBA module contains two procedures, each of which creates a classroom object called myClassroom. The first one, TestClassrooml, adds one student to the classroom then calls TestClassroom2, which creates a second classroom instance and adds two students.

Option Compare Database

Option Explicit

Public Sub TestClassroom1()

Dim MyClassroom As clsClassroom

Set MyClassroom = New clsClassroom

MyClassroom.AddStudent

MsgBox "I have " & MyClassroom.Students &

" student

in my class."

TestClassroom2

MsgBox "I still have only " & MyClassroom.

Students &

" student in my class."

End Sub

Public Sub TestClassroom2()

Dim MyClassroom As clsClassroom

Set MyClassroom = New clsClassroom

MyClassroom. AddStudent

MyClassroom. AddStudent

MsgBox "I have " & MyClassroom.Students &

" students

in my class."

End Sub

Both instances of the clsClassroom class are exactly the same in form and function, but are completely different entities. Thus, the properties of each are completely distinct from each other.

0 0

Post a comment