Share via


When Should You Create Custom Objects?

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following sections outline some situations in which it makes sense to create your own objects.

Reducing Code Complexity

Any time you find yourself writing code for a complex operation that you might have to use more than once, consider building an object. Remember that the object itself does not have to do anything new or profound; simplicity that will save you time in the future is sufficient justification. For example, if you are building an application that involves displaying data from a Microsoft® Access database on a Microsoft® Excel worksheet, you might want to create a combo box that can bind to data in the database. Rather than doing all the binding work every time you must have 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.

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. Because 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.

Building Custom Data Structures

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 application and a network server. Of course, you could also store this kind of data in a database; it depends on the requirements of your application. If you are working with small data structures, using a custom object might provide better performance.

Building Component Object Model (COM) Add-ins and Application-Specific Add-ins

If you are building a COM add-in, you will work with the class module provided by the add-in designer. You might want your add-in to provide custom events; at the very least, you might 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 section.

Custom objects can be useful in application-specific add-ins as well. They're a good way to add new functionality to a document.

Creating Custom Object Models

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 as 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. Because 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.

See Also

Designing Object Models | Why Build Your Own Objects? | Basic Class Concepts | Creating Property Procedures | Creating Events and Event Procedures | Extending Objects Through Interfaces | Designing Object Models | Creating Custom Objects for Web Pages | Add-ins, Templates, Wizards, and Libraries