1998-07-07 VBA programming 0 53
A class is the formal definition of an object. The class is a template for the creation of the object during programming, and defines the properties and methods that decides how the object behaves. Class modules is new in Office97. With class modules it's possible to create your own objects. These objects can have properties and methods like the built-in objects, and you can create several copies of an object if you need to.
Programmers have managed without class modules earlier, and the reason for using class modules may not become obvious before you have used class modules for a while. Here are some of the advantages when using class modules:
- Class modules makes it possible to separate complicated source code for advanced processes. This makes it easier for other to use the source code without understanding how the process is performed.
- Class modules makes the development of complicated tasks easier by breaking up the code in smaller and easier manageable parts. This has been possible earlier, but a class module forces you to separate the code from the ordinary procedures, resulting in a more obvious function separation.
- Class modules let you create reusable components. Because of the obvious separation between classes and the procedures using them, the classes contains independent code components that can easily be shared between different projects.
- Class modules is the foundation of other component technologies, Visual Basic can be used to create X Automation servers and ActiveX controls.
When to use class modulesHere are some examples on when you would want to use class modules:
- Database management
Class modules makes it easier to create objects that can manage a database with VBA. You can create a class that contains code for reading or writing to a database table. This class can be used in your macro without the user knowing how and where the data comes from.
- Wrapping in API procedures
Class modules makes it easier to use Windows API-functions in your macros. Wrapping the API-functions in a class module makes the development easier for people that are not that familiar with the more complicated function in Windows.
- Managing reading and writing to text files
This is really not that complicated, but who goes around and remember how this is done in VBA? By creating a class that manage low level reading to and writing from a text file, the properties and methods in the class can easily be used in your macros.