3 Replies Latest reply on Oct 16, 2007 9:39 AM by Gregory Lafrance

    dynamic array collection

    Chris Ellem - Sydney
      How do i create a dynamic array collection for use in the ages dropbox.

      convert this

      [Bindable]
      public var ages:ArrayCollection = new ArrayCollection(
      [{data:18},{data:19},{data:90}]);

      to display ages 1 -100?

      thanks in advance...

      chris

        • 1. Re: dynamic array collection
          Gregory Lafrance Level 6
          Don't know if this is what you had in mind:

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="initArray(1, 100);">
          <mx:Script>
          <![CDATA[
          import mx.collections.ArrayCollection;

          [Bindable]
          public var ages:ArrayCollection;

          private function initArray(start:int, end:int):void {
          var nums:Array = new Array();
          for(var x:int = start;x <= end; x++){
          nums.push(x);
          }
          ages = new ArrayCollection(nums);
          }
          ]]>
          </mx:Script>

          <mx:ComboBox dataProvider="{ages}"/>
          <mx:HBox>
          <mx:VBox>
          <mx:Label text="Start Age: "/>
          <mx:Label text="End Age: "/>
          </mx:VBox>
          <mx:VBox>
          <mx:TextInput id="newstart"/>
          <mx:TextInput id="newend"/>
          </mx:VBox>
          </mx:HBox>
          <mx:Button label="Change Ages" click="initArray(Number(newstart.text), Number(newend.text))"/>
          </mx:Application>
          • 2. Re: dynamic array collection
            FlexPrashant
            can i create a array like this :---

            function store(lbl:label):void
            {
            array[x][y]=label.text
            .............
            ..............
            }
            • 3. Re: dynamic array collection
              Gregory Lafrance Level 6
              I don't think the array [x][y] syntax is valid in Flex when assigning array elements, though I know it is valid when accessing array elements. I think it is necessary to somehow create one array, and then assign that array as an element of the second array, as in my code below. I have a line commented out as an alternative method of generating the inner array. Hope this helps.

              <?xml version="1.0"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
              <mx:Script>
              <![CDATA[
              private var arr1:Array = new Array();
              private var arr2:Array = new Array();
              private function store(lbl:Label):void
              {
              var x:uint = 0;
              var y:uint = 0;
              arr2[y] = lbl.text; arr1[x] = arr2;
              // arr1[x] = [lbl.text]
              }
              private function setLabel():void
              {
              lblTwo.text = arr1[x][y];
              }
              ]]>
              </mx:Script>

              <mx:Label id="lblOne" text="Word1 Word2"/>
              <mx:Label id="lblTwo" text="initial text"/>

              <mx:Button label="Store Text (Click First)" click="store(lblOne)"/>
              <mx:Button label="Set Label Text (Click Second)" click="setLabel()"/>
              </mx:Application>