Start Programming with Class

MAKE no mistake about it—the addition of class modulesis the most significant developer enhancement in Access 97 (and the rest of theOffice applications). What makes class modules so useful? Several benefits cometo mind. Class modules allow you to create your own object models that look andbehave just like Microsoft’s. So, instead of using wrapper functions such asCopyToClipboard and PasteFromClipboard, you can use syntax such asClipboard.SetText and Clipboard.GetText.

Another great thing about class modules is that youcan reuse your code— our objects—without having to worry about re-entrancy orstepping on your variables. That’s because each instantiation of your class iscompletely separate from all other instantiations. For example, say you have a simpletimer object that’s a wrapper around the Windows API timer calls. Using astandard module, you might place the starting time in a module- evel variable.But what happens if you call that code a second time before the first processis done? Either you have chaos, or you need to create a system, using an arrayor a collection, to track multiple clocks. If you use a class module, however,you don’t have to worry about any of this because each timer object isindependent of all other timers and has its own data (that is, variables).

In this issue’s leadarticle, veteran Access and VB programmerJim Ferguson walks you through the creation of a class that makes Access’unbound list boxes behave like VB’s. This is a perfect example of how much moreuseful classes are than standard modules. Imagine if this were done withstandard modules and you wanted to use the code for multiple list boxes on aform. With the class approach, this is all taken care of by VBA—automatically.

Access/Office 97’s class modules (as well as VB 4.0’s)are missing one significant feature: inheritance. Without inheritance, youcan’t create subclasses that inherit the properties and methods of their parentclass, thus nullifying one of the foundations of object-oriented programming. Ihope this serious shortcoming of VBA classes will be fixed sometime soon (thatis, by the turn of the century). Nonetheless, class modules are still a verysignificant addition to VBA and Access. As you upgrade your existing Accessapplications to Access 97, be sure to take advantage of them. Once you buy intoclass modules, you’ll wonder how you ever coded without them.

This entry was posted in Editorials. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.