Modules and Class Modules VB Help

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 (questions and 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 frol!l several places in your code and from within multiple. Forms, then you should implement it ina 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 SC.ope 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

Process and Out-of-Process Servers A Class Module is a server, or an application that provides its services to client applications. When you 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 th~ running EXE server. In other 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.

Posted on November 9, 2015 in Active Server Pages

Share the Story

Back to Top