8.1.2 Collection Objects

An object that manages a set of related objects is a collection. For example, the Sessions object manages individual Session objects; the Toolbars and QuickPad objects manage Toolbar and QuickPad objects. Objects within a collection are sometimes referred to as elements.

A collection, which consists of zero or more elements, includes a Count property that returns the current number of elements For example, the expression Sessions.Count returns the number of Session objects that are currently open.

To retrieve an individual element within a collection, you must identify the element with the collection's Item method. The Item method takes a numeric argument that represents the element's ordinal position within the collection. The Item property can also take a string argument that specifies an element's name. The following expressions show two ways to return Session 3, the third element in the Sessions collection object.

Sessions.Item("Session3")
-or-
Sessions.Item(3)

NOTE:Because Item is the default method of collection objects, you do not have to explicitly specify the method. Session 3 in the collection can also be referenced with Sessions(3).Close.

Like any returned objects, objects returned by properties and methods of collections can be assigned to object variables.

Example 8-1 Collection Objects Sample

A collection object makes it easy to iterate (perform repetitive actions) on all of the objects within the collection. Using Microsoft's Visual Basic, the For...Each statement is one way to iterate through the elements of a collection. For example, the following For...Next statement loops through all open sessions and minimizes their windows.

Dim Ses As Object
For Each Ses in infoconnect.Sessions
         Ses.WindowState = xMINIMIZED
Next