Visual Basic Concepts
Creating Your Own Collection Classes
There are three general approaches you can take to implementing object containment using collections. Consider the Employees collection of the SmallBusiness object discussed in "Object Models." To implement this collection you might:
In the SmallBusiness class module, declare an
Employees
variable As Collection, and make it Public. This is the cheap solution.In the SmallBusiness class module, declare an
mcolEmployees
variable As Collection, and make it Private. Give the SmallBusiness object a set of methods for adding and deleting objects. This is the least object-oriented of the three designs.Implement your own collection class, by creating a collection class module named Employees, as described later in this chapter. Give the SmallBusiness object a read-only property of the Employees class.
The strategies are listed in order of increasing robustness. They could be characterized as the house of straw, house of sticks, and house of bricks approaches.
Public Collection Example: The House of Straw The Collection object's very flexibility betrays it — you can put anything into a Collection, including the KitchenSink object.
Private Collection Example: The House of Sticks You can gain some robustness by making the Collection object private, but you lose the ability to use For Each … Next with the collection.
Creating Your Own Collection Class: The House of Bricks Creating your own collection class gives you the robustness of encapsulation, and as a bonus you get back the ability to use For Each … Next.
The Benefits of Good Object-Oriented Design Collection classes lead to cleaner, safer code.