PDA

View Full Version : assigning Flex variables


ljonny18
11-09-2006, 08:27 PM
HI I was wondering if someone could possibly help me ???

basically (as an example)....... I have an array that I want to "flatten" and put into an object..... e.g. :

var count:int = -1;
var flatObj:Object = new Object;
for(var x:int=0; x<myArray.length; x++)
{
count++;
flatObj.valueA_1 = myArray[count].valueA;
flatObj.valueB_2 = myArray[count].valueB;
flatObj.valueC_3 = myArray[count].valueC;
flatObj.valueD_4 = myArray[count].valueD;
}

but within the loop, I want to set the number valuse (valueA_1) = to the count value
e.g.
valueA_(count) etc.....

flatObj.valueA_(count) = myArray[count].valueA;
flatObj.valueB_(count) = myArray[count].valueB;
flatObj.valueC_(count) = myArray[count].valueC;
flatObj.valueD_(count) = myArray[count].valueD;

which sohud be the same ......

I just need to know, how I can add the count value to the string / test valueX_......
I assune ut is something like: valueA_{count} or valueA_#count# etc..... but I cant work it out :(

thanks again,
Jon.

meddlingwithfir
11-09-2006, 10:04 PM
So you're trying to create dynamic variable names, huh? I don't know off the top of my head, but I'll try some ideas I've got and report back.

meddlingwithfir
11-09-2006, 10:16 PM
Wow, this might be easier than I thought. Is this what you're looking for?


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="load();">
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.controls.Alert;

private function load():void
{
var myObject:Object = new Object();

var myID:String;
for(var i:int=0; i<5; i++)
{
myID = "id"+i;
myObject[myID] = myID.toUpperCase();
}

Alert.show( ObjectUtil.toString( myObject ) , "MyObject");
}
]]>
</mx:Script>
</mx:Application>

CDHBookingEdge
11-10-2006, 09:13 AM
I'm having some trouble getting a full gist of what you're meaning in some of this. For example, what type is valueA?

From looking at your for loop it looks like (on first glance) that you're going to be having (for example) valueA_1 overwritten time and time again.I mean think about it..let's say that myArray has a length of 3. This is what I see happening in your for loop during those 3 iterations thru it:
1) valueA_1 gets something in it like 1
2) valueA_1 gets something in it like 2
3) valueA_1 gets something in it like 3

Is that what you want to have happen? I'm thinking not, though I maybe am missing the gist of it. Sounds to me like what you want is an object/class that looks something like this:

public class flatObject
{
var valueA : Array = new Array();
var valueB : Array = new Array();
var valueC : Array = new Array();
var valueD : Array = new Array();
}


then you can just say something like:
var int myvariable = flatObect.valueA[1];

Is that what you need to get from this object/class?
Let's take it backwards, maybe that will help. What do you want the resultant flattened information to look like, and how do you want to or need to access it?

Christopher

CDHBookingEdge
11-10-2006, 09:20 AM
Oh! dynamic variable names! I got ya now. Sorry musta not had enough coffee yet. I didnt' get that on my first reading. ;-) And so what it seems like to me med is doing is making really a keyed list where the key is the var name. Guess what I'm missing is, what's the piece after this? What are you then gonna do with those vars?

med, I think we're both a little off on our interpretation here. LOL But I could be wrong, sometimes it feels like I am about every 30 seconds. *laughs*

Christopher

Tink
11-10-2006, 10:39 AM
var flatObj:Object = new Object();
var iterations:Number = myArray.length;
for( var i:int = 0; i < iterations; i++ )
{
flatObj[ "valueA_" + ( i + 1 ) ] = myArray[ i ].valueA;
}should do the trick