Object

Collection Class

Used to store a set of related items in a larger structure. The concept of a collection is similar to that of an array, except that each element in a collection can be a different data type.

Methods
Add
Count
Item
Remove

A collection can be thought of as an array of variants. Also, each element in a collection can be named. Elements in a collection can be referred to either by number or name.

The Dictionary class provides all the functionality of the Collection class and offers several advantages: With the Collection class, the time taken to locate an item is a function of the number of items in the Collection because the search is sequential. A Dictionary uses a hash table, making the time (relatively) independent of the number of items. It is designed for high-speed lookups. Also, the key parameter in a Dictionary is a Variant, but is a String in the Collection class. Therefore, we recommend that you use the Dictionary class rather than the Collection class whenever possible.

The Collection class is included mainly for backward compatibility and for conversion of Visual Basic projects.

# Example

The following example creates a collection, populates it with both String and numeric values, and displays each element in EditFields or a Canvas control (The picture "lois" has been added to the project). Note that a collection is much like a database record.

Dim c as New Collection
c.add 1,"ID"
c.add "Lois Lane", "Name"
c.add "Reporter", "JobTitle"
c.add 85000,"Salary"
c.add lois,"Picture"
EditField1.text=c.item("ID")
EditField2.text=c.item(2) //returns "Lois Lane"
EditField3.text=c.item("JobTitle")
EditField4.text=c.item("Salary")
Canvas1.backdrop=c.item("Picture")

If you want to use the Item or Remove methods to refer to an item, use parentheses around the parameter passed to the method. This is because the compiler doesn't know which data type you are passing. For example, use

c.remove("Name")

## rather than

c.Remove "Name"

to remove the "Lois Lane" record from the collection.

# See Also

Dictionary, Variant classes; VarType function; Nil keyword.