From Xojo Documentation
|You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!|
|The Collection class is included mainly for backward compatibility and for conversion of Visual Basic projects.|
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.
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 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.
c.Add("Lois Lane", "Name")
c.Add(lois, "Picture") // lois is a Picture added to the project
TextField1.Text = c.Item("ID")
TextField2.Text = c.Item(2) // returns "Lois Lane"
TextField3.Text = c.Item("JobTitle")
TextField4.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
to remove the "Lois Lane" record from the collection.