AS3 - Looping over properties of a class (property enumeration)
var myVO = new MyVO();
for(i in myVO){
trace(i+':'+myVO[i]);
}
However in AS3 this code will not work as expected (assuming that MyVO is not a dynamic class). I've come across this a couple of times in the last couple of weeks a yesterday found the solution to it.
There is a new function which gives all the introspection data for classes in AS3 - flash.util.describeType() which returns an XML Object.
flash.utils.describeType(myVO);
To do the previous looping over the properties it could be used as follows:
//and return the variable types as XMLList with e4x
var varList:XMLList = flash.utils.describeType(myVO)..variable;
for(var i:int; i < varList.length(); i++){
//Show the name and the value
trace(varList[i].@name+':'+ myVO[varList[i].@name]);
}
There is one other thing to catch you out though. If you make your variables [Bindable] or use getters & setters they will not show up as "variable". They will show up instead as "accessor" which is something to be aware of.
Hope it helps. Cheers, Mark




var def : XML = describeType(obj);
var properties : XMLList = def..variable.@name + def..accessor.@name;
for each ( var property : String in properties ) {
trace(property + ": " + obj[property]);
}
I haven't compiled nor tested the above, but I have done something similar a few times.
Thanks for the tip - I tried it out and it works great. I'm getting to like E4X more the more I use it.
Cheers,
Mark