ABSTRACT

Managing Data in Applications Every application needs to keep track of some amount of information. It may be contact information, a list of current movies, or a host of asteroids and bullets. As explained in Chapter 8, variables act as containers for different types of information. One way to provide storage for application data is to declare the appropriate variables. For example:

var currentUserID:String;

var numberOfAteroids:Number;

It would even be possible to declare enough variables to hold contact information for a group of individuals. Doing so might look like this:

var user1Email:String; var user2Email:String; var user3Email:String; var user4Email:String; var user5Email:String; var user6Email:String; var user7Email:String; var user8Email:String; var user9Email:String; var user10Email:String;

In this way, enough memory could easily be allocated for 10 e-mail addresses. The technique could even be used for hundreds or thousands of addresses. But the code would be unmanageable. And accessing the individual addresses would be cumbersome. In the early AS1 days, this technique was actually often used. It worked because variables exist as properties of the timeline on which they are defined. Since properties can be accessed by name using the [ ] operator, it is possible to access the above variables like:

var id:Number = 6;

this["user" + id + "Email"] = "joe@joe.com"; trace("Email (" + id + ") = " + this["user" + id + "Email"]);

The output would be:

Email (6) = joe@joe.com This technique is awkward and the code can easily become confusing. It is also very inefficient since ActionScript has to first construct each String property name, and then use this String to search for the requested property. The above example can be seen in the BeforeArrays.fla in the ch10 folder of the examples.