1

I have the following scenario:

if (event.status == AMFResultEvent.SUCCESS) {                
var lev1:uint = 0;
var lev2:uint = 0;
var lev3:uint = 0;
var lev4:uint = 0;
var lev5:uint = 0;
var lev6:uint = 0;

for (var i:int = 0; i < event.result.length; i++) {
    if (mainLevel == "1") {
        lev1++;
    }
    if (mainLevel == "2") {
        lev2++;
    }
    if (mainLevel == "3") {
        lev3++;
    }
    if (mainLevel == "4") {
        lev4++;
    }
    if (mainLevel == "5") {
        lev5++;
    }
    if (mainLevel == "6") {
        lev6++;                        
    }
}

for (var j:int = 1; j < 7; j++) {                
    _row = new StatisticsRow(event.result[j], this);
    _rowsPlace.addChild(_row);
    _row.y = (_row.height +1) * j;
    _row.codeLevel.htmlText = j; // works as it should
    // need to access variables lev1 - lev6, called by something like "lev"+j here:
    _row.amount.htmlText = 
}

// traces correct amounts of mainLevels from the i loop:
trace ("level 1: " + lev1);
trace ("level 2: " + lev2);
trace ("level 3: " + lev3);
trace ("level 4: " + lev4);
trace ("level 5: " + lev5);
trace ("level 6: " + lev6);            

}

I'm missing something obvious here, as the ["lev"]+j doen't work. How can I dynamically acces the lev1 - lev6 in the j-loop? As the code comment at the bottoms shows, this traces as expected.

Thanks in advance!

2 Answers 2

1

You can access them with brackets, string concatenation, and the this keyword. Here's an example of how you would use bracket notation in a loop:

for (var i:int = 0; i <= 6; i++) {
    var currLev = this["lev"+i];
    // do stuff to currLev
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. I apologize for my shortcomings on this topic, but I can't make it work. Firstly, should the var currLev be typed? And secondly, if I use the this keyword, I get Error #1069: Property lev1 not found on ClassName. I need to reference the variables in created at the top, by somehow adding the j in the j-loop.
Try to make your class 'dynamic'.
It is a pretty huge class, so I'd rather not change anything about the class it self, if I don't have to. Is there no way to reference the variables already created by manipulating the number in the variable name? If I write _row.amount.htmlText = lev1; that line parses as it should, but of course assigns the lev1 value to all rows. So I just need to alter the number in the end of the variable by assigning j from the j-loop. I assume the problem is syntax?
this needs to be a reference to the class with the lev* variables. If this loop is outside of that class, then you replace that with a variable name: ClassName c = new ClassName(); for (var i:int = 0; i <=6; i++) { trace(c["lev"+i]); }
Don't just copy my code. It's not exact for your situation, I was trying to explain the concept you needed to use (which is the this["lev"+i] notation). You can put that in your j loop and replace i with j and you'll have a reference to lev(j). I'll edit my answer to say it's not a specific solution to your problem, but more an example for bracket notation.
|
0

Thanks for answering guys!

I had a lousy approach to my problem anyway, and should have used an array right away:

var mainLevels:Array = new Array();

for (var n:int = 1; n < 7; n++) {
    mainLevels[n] = 0;
}

if (event.status == AMFResultEvent.SUCCESS) {

    for (var i:int = 0; i < event.result.length; i++) {

        var data = event.result[i];
        var correctCode:String = data["correct"];
        var mainLevelFound:uint = uint(correctCode.substr(0, 1));

        for (var k:int = 1; k < 7; k++) {
            if (k == mainLevelFound) {
                mainLevels[k]++;
            }
        }
    }

    for (var j:int = 1; j < 7; j++) {

    _row = new StatisticsRow(event.result[j], this);
    _rowsPlace.addChild(_row);
    _row.y = (_row.height +1) * j;
    _row.codeLevel.htmlText = j;
    // Now this works as a reference to mainLevels[*] created above!
    _row.amount.htmlText = mainLevels[j];
}

Thanks again for your effort :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.