Visual Basic Concepts
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.
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.
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."
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.
The following list summarizes the steps required to create a collection class.
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
.
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.