Modules and Class Modules

Code components are implemented in Visual Basic as Class Modules. Modules have existed in Visual Basic since its first version. They store variable declarations . and code (function  subroutines), which are available to all the other components
of an application. Many of the examples developed in previous chapters have their own Modules. If the function Convert Temperature(degrees As Double) is going to be called fro several places in your code and from within multiple. Forms, then you should implement it in a Module. Procedures stored in a Module can be called from any part of an application; the same is true for variables that must be accessed by multiple procedures. This is one method by which Forms and procedures in different Forms can exchange information.

You can think .of the procedures as methods of the Module only you don’t have to prefix them with the name of the Module when you call them. The public variables you store in a Module can also be thought of as properties of the Module. Therefore a Module is similar to a Class and Classes are implemented as special . types of Modules called Class Modules. A question some readers might raise at this point is “Why bother with Classes  when a Module can provide the functionality I need in my application?” If you only need a few procedures and global variables for a single project you’re probably better off dumping them in a Module. If you plan to use the same procedures in multiple projects however then you should build a Class Module. Let’s say you’ve implemented several functions in a Module, tested them with Project A and you’re satisfied with them. Later on you build another project Project B which uses the same Module. You realize that the functions in the Module need some tweaking so you introduce a few additional public variables and then the Module works well with Project B-but you’ve inadvertently broken Project A. And even if you’re disciplined enough not to touch the Module what if you belong to a group working on a common project? Allowing many programmers to access the same Module from within their projects is an accident waiting to happen. One of them will inevitably tweak the code and break some of the existing projects Another potential danger is conflicting names. If you add a Module with many public variables to a project some of the Module’s variables may have the same names as other variables you’ve used in y.our other project, In that case the scope rules will determine which variable is referenced (which may not be the one you think is being used). The properties in a Class Module can’t be accessed by their name only The must be accessed with an expression like class Name Variable Name
which makes the property unique not only in the project but in the operating system itself.

In-Process and Out-of-Process Servers

A Class Module is a server or an application that provides its services to client applications. When you create.an object variable to access the properties and methods of a Class you’re actually invoking an executable file (OLL or EXE)which runs in the background and waits to be contacted. Every time you set or read a property value or call a method this executable activates performs some actions and optionally returns some results to your application.
Servers can be implemented as ActiveX EXE or ActiveX DLL components, The difference between the two lies in how the server is executed. An ActiveX DLL component is an in-process server. The DLL is loaded in the same address space as the executable that calls the server and on the same thread as the client. At any given moment, however either the client application or the DLL is running. The benefit of DLLs is that they are faster because in effect they become part of the application that uses them.
An ActiveX EXE component is an out-of-process server that runs as separate process ..When a client application creates an object provided by an EXE server for the first time, the server starts running as a separate process. If another client application creates the same object the new object is provided by the running EXE server. In other odes a single EXE server can service multiple clients. Out-of process servers seem to be more efficient in resource allocation, but exchanging information between servers is a slow process, so execution speed in in-process servers is faster. The communication between two processes is a complicated
process known as marshaling.
An example of an out-of-process server is Excel. You can run two or more applications that request Excel’s services (as explained in the last chapter), and they will all be serviced by a single instance of Excel. For an application the size of Excel it  wouldn’t make much sense to invoke a new instance of it every time a Visual Basic
application needs to evaluate an expression with the Evaluate method. If the VB applications must access different worksheets they can all be-opened by the same instance of Excel.

VB Modules: A Quick Review

It’s usually easier to understand new concepts if you associate them with others you’ve already mast~. Let’s start by reviewing Visual Basic Modules and then see how the same concepts are applied to Class Modules: We’ll implement a short application that will count tune (it can become part of another application that needs to time its operations) ..J:p the application, we want to be able to start and
stop the timer at will. When the application executes auxiliary code, we should be able to pause the timer, then continue timing. After the operation to be timed completes the must be able to read the elapsed time. Let’s implement the timing logic in a Modules By implementing the g logic in a Module, we can reuse the component with other projects (ln general you shouldn’t include the logic of timing or other auxiliary operations in your code It’s best to isolate this-logic and make it available to your application with a few procedure calls). The application we’ll develop in this on is called Tuner Mod, and you’ll find it in this chapter’s’ folder on the CD.

Start a new project, add a Module to the project, and then insert the following lines in the Module.

The Timer Mod Modules code

The subroutine Start Counting () starts the timer and the subroutine Stop Counting() pauses the timer. Each time the timer is stopped the Total internet variable is updated. To find out the elapsed time the application must read the value of the variable. Finally the Reset Timer subroutine resets the timer. The timing logic doesn’t… allow you to read the elapsed time We the timer is running you joust first call the.Stop. Counting method t~ update the variable Total Interval then read the variable’s.value. Now switch to the application’s Form.and place two buttons on the Form. Set their captions to Start Tuning and Show Interval, as shown in Figure 15.1.Then insert the following lines in the Form’ s Code window.

The Timer Mod. Test Form’s Code

Private Sub Command1_Click()
If Command1.Caption – “Start” Then
Start counting .
Command1.Caption – “Stop”
Else
Stop Counting
Commandl.Capt;on – ·Start·
End If
End Sub

The Timer Mod
project’s Form

FIGURE 15.1
FIGURE 15.1

Run the application, start the timer, and stop and restart it a few times. Then click the Show Interval button to see the elapsed time in hours minutes and seconds. The code works, but there are a couple of problems. If another developer gets his hands on this Module, he may attempt to set the Total internal variable with a statement like:
Total Interval-O. 00453567 .. This is possible and the value shown is a valid time value, but what if he attempts to set the variable to an invalid value like: Total interval. – 99 The program will most likely work, but it will no longer report the correct elapsed time. Another programmer may ignore the Reset Tuner O subroutine and attempt to reset the Total interval variable from within the Start Counting subroutine. Perhaps he doesn’t really care about pausing and resuming the timing process and it suits him to reset the Total interval variable every time the Start Counting subroutine is called.
His project will work (with fewer calls too), but yours will no longer work. And t!-.is is the most important benefit of implementing auxiliary procedures as Class modules to create a component that’s consistent across multiple applications. In addition by implementing the timing logic separately and apart from the application you hide the details of its implementation from the developers who use it. In the same way you don’t have to know how Excel evaluates a complicated. math expression in order to call its Evaluate method.

 

Scroll to Top