Solved

Iterating over properties of a class Actionscript

Posted on 2011-02-18
6
588 Views
Last Modified: 2012-06-27
Hi I wonder if someone could help.

Still being fairly new to programming as a whole,

I have an incoming string to a function which i wish to validate against a static constant from a class.

The class is ListStyleType and it has numerous constants of the like ListStyleType.Decimal, ListStyleType.Box etc

Now I could create an array and place all the constants in that, and then loop over the values, but I dont really want to have to type out 50 odd values as the string constants already exist

Therefore I was wondering if there was any way I could loop of the properties of the class itself

ie something like

private function checkStyle(incomingString){
   for each(var property:String in ListStyleType){
       if (incomingString== property){
          etc.....
}
}
}

Thanks in advance
0
Comment
Question by:BloodGrinder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 34925148
What exactly are you trying to do?

You want to create an input that takes a string and if this string matches a constant you want it to execute something (each constant different action)?
0
 
LVL 19

Assisted Solution

by:Gary Benade
Gary Benade earned 100 total points
ID: 34925414
import flash.utils.describeType;

var yourObject:YourObjectType; // the object you want to iterate

var propxml:XML = describeType(yourObject);
for each( var prop:XML in propxml..accessor)
{
      trace( prop.@name, yourObject[ prop.@name]);  // property name, value
}
0
 
LVL 11

Assisted Solution

by:petiex
petiex earned 100 total points
ID: 34925475
What hobbit said, except replace propxml..accessor with propxml..constant
0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 300 total points
ID: 34925628
My solution would be a little different:

At fist create the functions that react on the constants:
function func1(obj:Object, other:Object):void {
   ... do something in here ...
}

function func2(obj:Object, other:Object):void {
   ... do something in here ...
}

function func3(obj:Object, other:Object):void {
   ... do something in here ...
}

function func4(obj:Object, other:Object):void {
   ... do something in here ...
}

Open in new window

Then initialize an Object which maps your constants to functions (Note in ActionScript functions are handled like variables):
var constMap:Object = new Object();
constMap['constant1'] = func1;
constMap['constant2'] = func2;
constMap['constant3'] = func3;
constMap['constant4'] = func4;

Open in new window

And now inside your checkStyle, simply check if there is a callback for the given input and eventually call it:
private function checkStyle(incomingString){
    if(constMap[incommingString]) {
        constMap[incommingString](incommingString, someOtherParameter);
    }
}

Open in new window

It's just an example ... but it works without loops (I hate loops ;-) ) and is pretty clean from an architectural point of view.

Hope this was what you were looking for.
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 34925653
Perhaps a little explanation at what's actually happening now:

If you call
checkStyle("hurz")

Open in new window

the lookup in line 2 of checkStyle will return null and nothing will happen.
If you however call
checkStyle("constant3")

Open in new window

Then the lookup will return a reference to the func3 function.
Now the function is incoked and the string itself and any other parameter is passed and the function is executed. You could also return a value if you needed to. I just did a void function, but any other type of function would work.
0
 

Author Closing Comment

by:BloodGrinder
ID: 34941014
Both Solution worked
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question