2 Replies Latest reply on Aug 25, 2010 10:07 PM by BhaskerChari

    using a variable to refer to an object's ID

    aklyman Level 1

      Is there a way to replace “comboBox1” in the following line of script code with a variable which contains the text “comboBox1”?


           comboBox1.dataProvider = dataArray;

      I was thinking something like this, but of course this doesn’t work---doesn't like the nameArray[i] before the dataProvider:

       

      function setProvider():void{
      var tempArray = [“temporary content”];
      var nameArray:Array = [“comboBox1” ,“comboBox2”,“comboBox2”];

       

      for (i =0, i ; nameArray.length; i++){

      nameArray[i].dataProvider = tempArray;

      }

      }

       

      <mx:ComboBox id="comboBox1"/>

      <mx:ComboBox id="comboBox2"/>

      <mx:ComboBox id="comboBox3"/>

       

       

      Thanks All

        • 1. Re: using a variable to refer to an object's ID
          GordonSmith Level 4

          You can exploit the fact that you can look up any property on any object by name, using obj[prop] notation. Applied to this case, it means that comboBox1 is the same as this["comboBox1"].

           

          Gordon Smith

          Adobe Flex SDK Team

          • 2. Re: using a variable to refer to an object's ID
            BhaskerChari Level 4

            Hi inanealex2010,

             

            You can use the following approach...

             

            function setProvider():void{
            var tempArray = [“temporary content”];
            var nameArray:Array = [“comboBox1” ,“comboBox2”,“comboBox2”];

             

            for (i =0, i ; nameArray.length; i++){

            //Use this line if you are placing the ComboBoxes directly in the root tag of your component

            ComboBox(this.getChildByName(nameArray[i]) ).dataProvider = tempArray;

            //Use this line if you are placing the ComboBoxes in any container say like canvas, Vbox or HBox

            ComboBox(containerId.getChildByName(nameArray[i]) ).dataProvider = tempArray;

            }

            }

             

            <mx:ComboBox id="comboBox1"/>

            <mx:ComboBox id="comboBox2"/>

            <mx:ComboBox id="comboBox3"/>

             

             

            Thanks,

            Bhasker