5.4. Collection Objects
Many of the classes that make up an object model are in fact collections of other classes or objects. You will find that these classes have several generic methods and properties, such as Add, Item, and Count. The Add method creates a new member of the collection stored against a unique key, the Item method returns a member of the collection given a unique key or index value, and the Count method returns the number of members in the collection.
You may find, however, that the collection object's standard Item method has been wrapped within a property, the name of which is the same as the name of the class held by the collection. A call to this property returns an object of the type of the class held by the collection. For example, an Employees collection object may implement an Employee property that returns an Employee object, like this:
Public Property Get Employee(vEmpCode As Variant) As Employee
Set Employee = McolEmployees.Item(vEmpCode)
End Property
Collections within object models are most commonly at the top of an object hierarchy or at the top of a branch of an object hierarchy, and are most likely createable with the New keyword or the CreateObject function. Access to individual objects is gained through the collection. For example, the Worksheets object spawns a Worksheet object; a Cells object spawns a Cell object.
5.4.1. Referencing by Name or Number
A correctly implemented collection object allows you to access the members of the collection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access