views:

61

answers:

2

I am creating a two-dimensional list that has consecutive numbers at the end of "day", for use as dataProvider for a DataGrid i have been accessing them via the command
dg1.selectedItem.day1
dg1.selectedItem.day2
dg1.selectedItem.day3
etc...

is there any way to take the string ("day"+i) and convert it into a (what is it? variable name?) so that i can do something along the lines of:

for(var i:Number=1; i<numFields; i++)
{
  dg1.selectedIndex = i-1;
  dg1.selectedItem.(mysteryFunction("day"+i)) = 42;
}

if there's a function that i could use for mysteryFunction, or what data type to use, any info would be very helpful


this is what i've been using (so tedious):

<mx:XMLList id="sched">
  <field>
   <day1></day1>
   <day2></day2>
   <day3></day3>
  </field>
  <field>
   <day1></day1>
   <day2></day2>
   <day3></day3>
  </field>

  ...
</mx:XMLList>
A: 

Use an Array or if you are going to bind it (which I am kind of betting on) use ArrayCollection instead of naming these variables individually.

If the members are generated by some program, you better put all of these in one of the collection classes I mentioned above and then start the processing. It makes life easier in the long run.

E4X is the way to go when dealing with XML. The Mozilla guys have an arguably better explanation of that technology. So, if your XML is stored in a variable as:

var tree:XML = <field>
    <day1></day1>
    <day2></day2>
    <day3></day3>

You can simply do:

tree.day1 = 42;

Why would you want this mysteryFunction()? A dataProvider object is just a collection of some Type. You know the type already, right? Read this.

Anyway, there is no such mystery function. Note, however, string concatenation with a number converts the number to a string. Try

trace("str " + 42);
dirkgently
+1  A: 

The "mystery function" you are looking for is as simple as indexing with brackets:

for(var i:Number=1; i<numFields; i++)
{
    dg1.selectedIndex = i-1;
    dg1.selectedItem["day"+i] = 42;
}

And it is called, surprisingly, an attribute.

David Hanak