Brain Dump

Thoughts on my latest research, recently discovered tricks and so on.
Flash, PHP, MySQL, Linux and all things Internet.

Collections in ActionScript
Sunday, February 04, 2007

An undocumented feature of ActionScript 2 is that list collection classes are possible. For the past year or so I've been using them and it's saved me a lot of time and headaches.

Other languages have had them for eons, but for those just getting acquainted with the idea, a (list) collection in this context, is simply an array of one type of object. Specifically, the collection only allows a specific type of object to be added to it, and the collection itself is a strictly defined type of object. That means, that when you encounter a collection that exists in someone's code, you can be assured that you know exactly what is inside it, whereas an array can hold anything.

It is quite reasonable for human error to cause you to unintentionally add a string object into an array that you intended to only hold numbers for example. Secondly, it is quite possible for you to accidentally pass the wrong array to a function. Collections save you from these headaches before they get buried like a needle in a haystack. That is, the Flash compiler will catch these mistakes early. If you're using MTASC, the error messages are even more helpful.

Imagine you have an address book class with a property called people in which you want to contain a list of PersonVO objects (see "Introducing the Value Object / Data Transfer Object Pattern" here to learn about using value objects. Using a collection your property would be strictly typed.

Your compiler would throw an error if you tried to pass anything other than a PersonVO object to the function.

Here is the source code that demonstrates the above entry

2 Comments:

At 2/05/2007 08:40:00 PM, Anonymous Jay said...

Some useful stuff here. Saw you at the Vancouver Flash Meetup last Thurs - lots of great stuff there too. Any chance of posting the code you presented?

 
At 2/05/2007 09:50:00 PM, Blogger Andrew Blair said...

Thanks Jay! Yep, I should have it up somewhere soon. Stay tuned on this blog for it.

 

Post a Comment

<< Home