8 Replies Latest reply on Feb 7, 2012 8:28 AM by geisergd

    variable that references an object

    geisergd

      In general, I am trying to use a variable to refer to an object.  On the Flex 3 platform, I have about 100 radio button groups.  I want to loop through data that has been returned from the database and turn the radio buttons on or off based on the data.

      public function ....

      var counter:Number = 1;

      var aString:String = "";

      var qString:String = "";

      loop......

      sString = ("A"+counter);

      qString = ("q"+counter);

      if (returnArray[0].aString == "0"){

      qString.selectedValue = "agree";

      }

       

      counter++;

       

      My radiobutton objects are named q1, q2, q3, etc.  The string qString.selectedValue = "agree"; is not evaluating as an object name.  I have tried .valueOf() and still generate errors.  I know I am trying to do something stupid.  Can someone please enlighten me?

        • 1. Re: variable that references an object
          PierreSolutions Level 2

          Can't tell exactly what you are itirating through from your code, but I would look at something like this

           

          var counter:int = 1;

          var totalNumber:int = ???

           

          for (counter = 1; counter < totalNumber; counter ++)

          {

               aString = 'a' + counter;

               qString = 'q' + counter;

               this['qString'].selected = (returnArray[counter].aString == '0');

          }

           

          the interesting part is the this['qString'] syntax ... which allows you to reference an object by passing its name.

          like your current form is an array containing all your UI.

          • 2. Re: variable that references an object
            geisergd Level 1

            I guess I didn't ask the question very well.  Normally one would type

             

            q1.selectedValue = "agree";   to select the q1 radio button that was labeled "agree".

             

            I would like to replace the q1 with a variable that contains the q1 object name in a string, but Flexbuilder returns an error about the variable not being able to reference the selectedValue of qString.  I will try the "this" syntax.

            • 3. Re: variable that references an object
              geisergd Level 1

              Thank you, thank you, thank you!  I modified the command slightly to this[qString].selectedValue = "agree"; and it worked great.  I have always looked at "this" as being a coldfusion scoping keyword.  I see that it is much more.  Thanks again.

              • 4. Re: variable that references an object
                PierreSolutions Level 2

                You are welcome.

                 

                I was just thinking I made a mistake, which you corrected.

                I told you to use this['qString'].selectedValue... but it should be this[qString].selectedValue (without the quotes because qString is a string variable holding the name of the UI element.).

                • 5. Re: variable that references an object
                  geisergd Level 1

                  I have now run into another related syntax issue.  When I try to use the same syntax in a reference to an arrayCollection I am failing. see code:

                   

                  allData is an arrayCollection, that in index[0] has values for A1, A2, A3, etc.

                   

                  public function handleQAResults():void {

                  var counter:Number = 1;

                  var aVal:String = "";

                  var qVal:String = "";

                    while(counter <5) {

                       aVal = ("A"+counter);

                       qVal = ("q"+counter);

                   

                       if (allData[0].this[aVal] == "0") {

                            this[qVal].selectedValue = "absolutely disagree";

                       }

                    }

                       counter++;

                  }

                   

                  If I hardcode (allData[0].A1 == "0") the code works, but I want to loop through A2, A3, etc.

                  I have also tried (allData[0].aVal == "0")

                  (allData[0].aVal.valueOf() == "0")

                  and a great combination of double quotes, single quotes, parentheses, and so on.

                  The code inside the IF statement works.  The IF statement itself doesn't.

                  • 6. Re: variable that references an object
                    PierreSolutions Level 2

                    try allData[0][aVal]

                     

                    where aVal is 'A1', then 'A2' etc ...

                     

                    it should be the equivalent of allData[0].A1

                     

                    the syntax using [] is another way of using .

                     

                    example

                     

                    var obj:Object = new Object();

                    obj.name = 'joe';

                    obj.age = 33;

                     

                    you can access name like

                    theName = obj.name;

                    or

                    theName = obj['name']; // notice quotes here because using actual value, not variable holding the name of the value.

                     

                    theAge = obj.age;

                    or

                    theAge = obj['age']; // notice quotes here because using actual value, not variable holding the name of the value.

                     

                    the [] is an alternative to dot notation, this adds a whole lot of flexibility to your code. I like that feature a lot.

                    • 7. Re: variable that references an object
                      geisergd Level 1

                      Thank you.  I will go back to my desk and give it a try....then update the discussion.

                      • 8. Re: variable that references an object
                        geisergd Level 1

                        That was it.  One little period.  Thank you very much.  You have been very helpful.