Chapter 4. Class Modules

To some degree, class modules can be seen as "replacements" for code modules—that is, class modules are repositories for shared variables as well as for shared code, just as code modules are. So why have class modules? What's wrong with normal code modules? Basically, there is nothing wrong with them. But code modules allow you to share procedures only within the project in which they reside. For example, you can call a public function from another code module in a project, but you can't call that function from another project. To do that, you have to add the code module to your project or, even worse, create a second copy of the code module.

Class modules have (without wanting to sound too evangelistic) revolutionized VB. The whole style of writing VB programs has changed since version 4.0 of VB was launched, bringing the VB/VBA language closer than ever to being a true object-oriented language. In fact, much of VB's current success in the corporate marketplace can be directly attributed to the ability to create ActiveX components, the cornerstone of which is the class module.

When you create a class module, you are creating a COM interface. Therefore, class modules allow you to describe your application to the outside world via a programmable interface that consists of properties, methods, and events in a way that allows you to retain control over the application. Using class modules, you can break an application into logical sections, each having its own class. This is the concept of encapsulation—everything having to do with a particular thing held within one wrapper—which is critical to object-oriented programming.

Sidebar 1. VBA and VB Class Modules

In most cases, a VBA class module is identical to a VB class module; code from VBA class modules can be imported into VB, and vice versa. The sole exception is custom-defined events, which missed the release of VBA5 in Microsoft Office 97. What you can do with a class module, though, is different in VBA and VB. You can't compile a VBA class module into a standalone DLL or EXE; for this you need the retail version of VB.


Finally, we have all heard and read loads about the "Holy Grail" of code reuse. OK, anyone can reuse any code; simply copy and paste a procedure from your last project, or create a .bas file containing a library of useful stuff you simply include in every project (VB3 programmers will remember constant.bas). The problem with this approach is that the source code has to be included in the project each time. What happens if you find a new, more efficient way to perform a particular function? You have to revisit each project to change all of the instances of the same code. However, if you'd used a class to create your library, you could simply add a reference to your class to any new project. This gives you the advantage of not allowing the source code to be seen (or changed) in the current project, and of only having to make a change to a procedure in one place. Class modules therefore allow you to reuse code in a structured manner in your own projects, and others can use them in their classes without having access to your source code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset