User Guide

Accessing collection information programmatically 963
Accessing collection information programmatically
Flash provides programmatic access to collection data through the Collection and Iterator
interfaces. The Collection interface lets you add, modify, and remove items in a collection. The
Iterator interface allows you to loop through the items in a collection.
There are two scenarios in which to use the Collection and Iterator interfaces:
Accessing collection information in a component class (AS) file” on page 963
Accessing collection items at runtime in a Flash application” on page 964
Advanced developers can also create, populate, access, and delete collections programmatically;
for more information, see “Collection interface (Flash Professional only)” on page 169.
Accessing collection information in a component class (AS) file
In a component’s class file, you can write code that interacts with collection items defined during
authoring or at runtime.
To access collection item information in a component class file, use any of the following
approaches:
The Collection tag includes a variable attribute, for which you specify a variable of type
mx.utils.Collection. Use this variable to access the collection, as shown in this example:
[Collection(name="LinkButtons", variable="__linkButtons",
collectionClass="mx.utils.CollectionImpl", collectionItem="ButtonC",
identifier="ButtonLabel")]
public var __linkButtons:mx.utils.Collection;
Access an iterator for the collection items by calling the Collection.getIterator() method,
as shown in this example:
var itr:mx.utils.Iterator = __linkButtons.getIterator();
Use the Iterator interface to step through the items in the collection. The Iterator.next()
method returns type Object, so you must cast the result to the type of your collection item, as
shown in this example:
while (itr.hasNext()) {
var button:ButtonC = ButtonC(itr.next());
...
}
Access collection item properties, as appropriate for your application, as shown in this example:
item.label = button.ButtonLabel;
if (button.ButtonLink != undefined) {
item.data = button.ButtonLink;
}
else {
item.enabled = false;
}