Share via


Visual Basic Concepts

The Benefits of Good Object-Oriented Design

This topic summarizes the results of the code example begun in "Public Collection Example: The House of Straw," and continued in "Private Collection Example: The House of Sticks" and "Creating Your Own Collection Class: The House of Bricks." You may want to read those topics before beginning this one.

Creating the Employees collection class results in a very clean, modular coding style. All the code for the collection is in the collection class (encapsulation), reducing the size of the SmallBusiness class module. If collections of Employee objects appear in more than one place in your object hierarchy, reusing the collection class requires no duplication of code.

Enhancing Collection Classes

You can implement additional methods and properties for your collection classes. For example, you could implement Copy and Move methods, or a read-only Parent property that contains a reference to the SmallBusiness object.

You could also add an event. For example, every time the Add or Remove method changed the number of items in your collection, you could raise a CountChanged event.

Robustness, Robustness, Robustness

You don't always have to implement collections in the most robust way possible. However, one of the benefits of programming with objects is code reuse; it's much easier to reuse objects than to copy source code, and it's much safer to use robust, encapsulated code.

A wise man once said, "If you want to write really robust code, you have to assume that really bad things will happen."

Collection Classes and Component Software

If you're using the Professional or Enterprise Edition of Visual Basic, you can turn your project into an ActiveX component, so that other programmers in your organization can use the objects you've created.

Steps to Implement a Collection Class

The following list summarizes the steps required to create a collection class.

  1. Add a class module to your project, and give it a name — usually the plural of the name of the object the collection class will contain. (See "Naming Properties, Methods, and Events" earlier in this chapter.)

  2. Add a private variable to contain a reference to the Collection object your properties and methods will delegate to.

  3. In the Class_Initialize event procedure, create the Collection object. (If you want to defer creation of this object until it's needed, you can declare the private variable in step 2 As New Collection. This adds a small amount of overhead each time the Collection is accessed.)

  4. Add a Count property and Add, Item, and Remove methods to your class module; in each case, delegate to the private Collection by calling its corresponding member.

  5. When you implement the Add method, you can override the behavior of the Collection object's undiscriminating Add method by accepting only objects of one type. You can even make it impossible to add externally created objects to your collection, so that your Add method completely controls the creation and initialization of objects.

  6. Use the Procedure Attributes dialog box to make the Item method the default for your collection class.

  7. Add a NewEnum method, as shown below. Use the Procedure Attributes dialog box to mark it as hidden, and to give it a Procedure ID of –4 so that it will work with For Each … Next.

    Public Function NewEnum() As IUnknown
       Set NewEnum = mcol.[_NewEnum]
    End Function
    

    Note   The code above assumes that the private variable in step 2 is named mcol.

  8. Add custom properties, methods, and events to the collection class.

Note   The Class Builder utility, included in the Professional and Enterprise editions of Visual Basic, will create collection classes for you. You can customize the resulting source code.

For More Information   You can read more about software components in Creating ActiveX Components, in the Component Tools Guide.