Creating Collection Classes

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.

You can represent a collection with a class module, and use the collection to organize objects of the same type. By convention, a collection class is usually given the plural name of the object that it contains; for example, the BoundLists collection contains BoundList objects.

Collection classes usually contain a Count property, for counting the number of objects in the collection, and an Item property, which returns a reference to an object in the collection by its index or key value. Most collection classes also contain an Add method, which adds a new object to the collection and returns a reference to it, and a Remove method, which removes an item from the collection.

You should consider how a top-level collection or object is to be initialized. Somewhere in your project there must be a line of code that creates an instance of the top-level collection or object. From there all other objects can be created from within your object model. If the collection will contain objects that might already exist or are saved with the file, such as bound list boxes or combo boxes, the collection will probably have to be initialized when the application loads. If the collection provides objects on demand, it might not have to be initialized until the objects are required.

You should also consider how objects are to be added to a collection. Does this happen based on user or programmatic input, or based on some change in the state of the system, or both? In addition, do objects have to be added when the application is first loaded?

See Also

Planning the Object Model | Relating Objects with Object Properties | Sharing Code by Using Interfaces | 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