Collection

From Xojo Documentation

Class (inherits from Object)

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. 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.

Methods
Add Item Remove
Count Key

Notes

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.

Sample Code

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

Var 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") // lois is a Picture added to the project
TextField1.Value = c.Item("ID")
TextField2. Value = c.Item(2) // returns "Lois Lane"
TextField3. Value = c.Item("JobTitle")
TextField4. Value = 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 datatype