Microsoft Office 2000/Visual Basic Programmer's Guide |
The following sections outline some situations in which it makes sense to create your own objects.
Any time you find yourself writing code for a complex operation that you may need to use more than once, consider building an object. Remember that the object itself doesn't have to do anything new or profound; simplicity that will save you time in the future is sufficient justification. For example, if you're building a solution that involves displaying data from a Microsoft Access database on an Excel worksheet, you may want to create a combo box that can bind to data in the database. Rather than doing all the binding work every time you need such a combo box, you can create a new object that has all the functionality of a combo box but also has methods and properties for binding data. For an example that shows how to build such an object, see the ListComboWiz.xla sample add-in in the ODETools\V9\Samples\OPG\Samples\CH11 subfolder on the Office 2000 Developer CD-ROM. This sample add-in includes a custom BoundList object and BoundLists collection, which are used to create data-bound list and combo boxes on an Excel worksheet.
Calls to the Windows application programming interface (API) and other DLLs lend themselves to being encapsulated in class modules. Creating a class module that handles DLL calls is sometimes called "wrapping" the DLL. Since DLL calls are often complicated, you can write the code to call a DLL function from within a class module, and use it in the future without having to remember the details of calling the DLL function directly. For example, you might create a System object with properties and methods that call various API functions internally to set and return information about the operating system. The System.xls sample file, found in the ODETools\V9\Samples\OPG\Samples\CH10 subfolder on the Office 2000 Developer CD-ROM, wraps some system-related API calls.
A custom object can act as a data structure, which is a group of related data stored as a unit. For example, you might create a Server object, with properties such as UNCPath and Available, and methods such as SaveFile, and use this object to manage operations between your solution and a network server. Of course, you could also store this kind of data in a database; it depends on the needs of your solution. If you're working with small data structures, using a custom object may provide better performance.
If you're building a COM add-in, you'll work with the class module provided by the add-in designer. You may want your add-in to provide custom events; at the very least, you may want an event procedure to run in response to the click of a command bar button, which involves some of the concepts discussed later in this chapter.
Custom objects can be useful in application-specific add-ins as well. They're a good way to add new functionality to a document. For example, the ListComboWiz.xla sample add-in mentioned earlier in this section copies the BoundList and BoundLists classes, which are used to rebind the lists, to an Excel workbook, and then uses these classes in code that runs when the workbook opens.
For more information about creating COM add-ins and application-specific add-ins, see Chapter 11, "Add-ins, Templates, Wizards, and Libraries."
You can create a custom object model to represent relationships between different parts of your application. Your custom object model can include objects and collections, just like any built-in object model does. The key to creating a custom object model is that you can use a class module to represent a collection. Since a collection is just an object that groups other objects of a particular type, you can write code within a class module so that the class module acts as a collection.
This chapter does not delve extensively into creating custom object models. The "Designing Object Models" section later in this chapter provides some suggestions and considerations for designing an object model. Two sample files on the Office 2000 Developer CD-ROM, ListComboWiz.xla and EnumWindows.xls, include examples of custom object models. Additionally, the "Where to Go from Here" section at the end of this chapter points to resources that provide greater detail about creating object models.