2 Replies Latest reply on Jul 26, 2006 7:41 AM by

    Eval Replacement?

      I know this has been addressed before, but i can't seem to get the [ ] to work for me in Flex.

      To dynamically access a series of text fields using eval(), i would have, in a loop, done the following:

      var fieldName = "field_"+i;
      myValue = eval(fieldName).text

      trying the following in Flex (AS 3.0), i cannot seem to get the value of the field:

      var fieldName = "field_"+i;
      myValue = [fieldName].text

      has anyone found a solution to this?
        • 1. Re: Eval Replacement?
          Level 7
          > var fieldName = "field_"+i;
          > myValue = eval(fieldName).text
          >
          > trying the following in Flex (AS 3.0), i cannot seem to get the value of
          > the
          > field:
          >
          > var fieldName = "field_"+i;
          > myValue = [fieldName].text

          no .. that is incorrect syntax for what you want .. that is getting the
          .text member of an array with a single string in it (the value of fieldName)

          the closest you can get is

          > myValue = this[fieldName].text

          However, that will not give you local variables (declared with 'var' or
          passed as function args). Without eval, you cannot get a local variable
          using a string for its name

          But, that said, you can write you code so you do not NEED ot do that .. os
          its not a great loss .. indeed, other techinques are clearer than using eval
          in that way.
          --
          Jeckyl


          1 person found this helpful
          • 2. Re: Eval Replacement?
            Thank you! This exactly solved my problem. I had tried:

            myValue = this.[fieldName].text
            (should have been)
            myValue = this[fieldName].text

            but of course had added a dot separator where i shouldn't have.

            thanks for the help!