Home > Language Reference > Classes
Collection Class
+ Object
+ Iterator
+ Collection
Description
The Collection class implements all the necessary functionality needed to manage an ordered collection of elements, either by their index or by key. Contrary to arrays, the elements in a collection can be of different types. This class is instantiable, derivable and cloneable.
You can create a collection just like any other object. For example:
Dim c as New Collection
You can then add elements to the collection with the Add method and delete them with the Remove method. The number of elements in a collection can be obtained using the Count property. A specific element can be retrieved using the Item function or simply tested for presence using the Has function, whilst it is possible to list the elements in a collection by using a For Each...Next loop.
The Clear method deletes all elements from a collection. Calling this method is faster than reallocating a new Collection object as it is common to do in other programming languages.
Finally, it is possible to sort elements in a collection with the Sort...On... statement.
When you duplicate a Collection object either implicitly or explicitly with the Clone keyowrd, the collection of elements are also copied. However, elements that are objects are not duplicated; only the reference is copied which means that the two collections reference the same objects in memory.
The number of elements in a collection is limited to 4096. On many physical devices, the actual available memory lowers this limit.
The following example shows how to use a collection to store then display the names of all the applications on the handheld device:
Private Sub Button1_Click()
Dim di as New DatabaseInfo
Dim c as New Collection
Dim b as Boolean, s as String
b = di.FindFirstByTypeCreator("appl","")
While b
c.Add di.Name
b = di.FindNext
Wend
For Each s in c
MsgBox s
Next s
End Sub
Members
| Members | Description |
| Add | Adds an element to a collection. |
| Clear | Deletes all elements in a collection. |
| Count | Number of elements in a collection. |
| Has | Tests whether an element with a given key exists in a collection or not. |
| Item | Retrieves an item from a collection. |
| Key | Retrieves the key associated to a collection item. |
| Remove | Deletes an element from a collection. |
| Inherited from Iterator | Description |
| NextItem | Event sent to advance to the next element. |
| Inherited from Object | Description |
| ClassID | Returns the type identifier corresponding to the actual class of the object. |
| Implements | Determines whether the object implements the features of a given class. |
| Iterate | Event raised to iterate over the elements of a container object. |
| Recipient | Recipient of events sent by the object. |
| Serialize | Event raised to serialize the object content into a stream. |
System requirements
| System | Minimal version | Remarks |
| Palm OS | Palm OS 3.0 | N/A |