There are two features of a normal Collection that the custom collection still doesn't support: a default method and For Each.
Default Method
First, it doesn't have a default method. That means you can access an item as in:
value = custom_collection.Item(1)
But you cannot get theitem like this:
value = custom_collection(1)
Follow these steps to make the Item function the colection's default method.
- Open the code window for the custom collection class.
- Select the Tools menu's Procedure Attributes item.
- On the dialog, click the Advanced >> button.
- In the Name dropdown, select the Item function.
- In the ProcedureId box, select (Default).
- Click OK.
For Each
The custom collection also doesn't support For Each. To use a For Each loop, Visual Basic needs an enumerator function to help it loop through the items in the collection. If you try to execute a For Each loop on the custom collection at this point, you'll receive the error:
Object doesn't support this property or method
The only way to provide this function in Visual Basic 6 is to use the enumerator of a normal Collection object. This is the reason the custom collection stores its values in a private Collection. If it were not for this issue, you could store the values in some other data structure such as a tree, linked list, hash table, or whatever.
To provide the enumerator function, add the following property procedure:
|