views:

42

answers:

1

Hi,

Can I call a function when building an array in Flex 3?

public function gridBuilder(myArray:Array):void {
            var i:uint; 
            for (i=0; i<myArray.length; i++){

            dGArray = [
                {Name: myArray[i].name, Type: 'A:', Score: myArray[i].score, Rank: myArray[i].rank, Grade:(myFunction(myArray[i].rank, myArray[i].max_rank))},
                {Name: myArray[i].name, Type: 'B:', Score: myArray[i].score, Rank: myArray[i].rank }
                                    ]   

                    }

                      dgAC = new ArrayCollection(dGArray);

                }

MyArray is the result of a remote call to the database. I then prepare the array to be used in a dataGrid. I also want to call a function that provides a grade. Unfortunately, my function appears to be called only once. Is it possible to call a function when you're building an array? Please see the "Grade:" bit. What's the problem? How do I solve this problem?

Thank you!

-Laxmidi

A: 

Hi there. You stated that your function was only called once. Yet in your code you are only explicitly calling it once. I am having trouble seeing your issue. In ActionScript, you can create an Array of objects, where attribute values can come from return values of functions.

EDIT Change your code to do this:

public function gridBuilder(myArray:Array):void {
    var i:uint; 
    var dGArray:Array = [];

    for (i=0; i<myArray.length; i++) {
        dgArray.push({Name: myArray[i].name,
                      Type: 'A:',
                      Score: myArray[i].score,
                      Rank: myArray[i].rank,
                      Grade:myFunction(myArray[i].rank,myArray[i].max_rank)});
        dgArray.push({Name: myArray[i].name,
                      Type: 'B:',
                      Score: myArray[i].score,
                      Rank: myArray[i].rank});
    }
    dgAC = new ArrayCollection(dGArray);
}

The problem with your original code was that you kept resetting your dGArray on each iteration.

clownbaby
Hi clownbaby, Thank you for the message! I'm still trying to figure out how I'm messing up. The myFunction fucntion uses a return value. The myArray array has 3 names. My goal was to have the myFunction called 3 times-- once for each i. Why isn't the myFunction getting called more than once? Thanks for clearing up that it is possible to make an array of values returned from functions. Again, thank you.
Laxmidi
Thanks, clownbaby. That problem was driving me nuts. Now I understand what I'm doing wrong. Thank you!
Laxmidi