Creating a Collection in a Class Module

Collections can be created in a class module but, in this case, the innate methods of the collection (Add, Remove, Count, Item) are not available; they have to be created in the class module. The advantages of creating a collection in a class module are that the entire code is in one module, you have more control over what is done with the collection, and you can prevent access to the collection.

Insert a new module for the collection and rename it clsEmployees. Declare a private collection to be used within the class module: Option Explicit

Private AllEmployees As New Collection

Add the new properties and methods required to make the collection work. The innate methods of collection are available within the class module and can be used to create the custom methods and properties:

Insert an Add method for adding new items to the collection: Public Sub Add(recEmployee As clsEmployee)

AllEmployees.Add recEmployee, recEmployee.EmpID

0 0

Post a comment