5 Replies Latest reply on Mar 13, 2007 8:18 PM by kglad

    Can I dynamically create variables ?

    Flamebg Level 1
      same as topic.. Example:

      for (i=1;i<10;i++) {
      var this["myVariable"+i]:Number = 5;
      }
      I know this won't work and create for me 9 variables and all of them with value of 5 and exactly cuz of that I am asking if there is a way to dynamically create variables .. so I don't have to type manually 100 different variables that I need....
      So.. is it possible and how ?

      thanks
        • 1. Re: Can I dynamically create variables ?
          kevicus
          i'm no expert on this, but perhaps there is some way to accomplish this using a class?
          • 3. Can I dynamically create variables ?
            Flamebg Level 1
            kglad, is this going to create the variable myVariable4 for example..
            cuz I think it will only make the already existing variables (myVariable1 till myVariable9) have value of 5.
            And my point is not to give the existing variables a value, but to create these variables, so I can use them in my script later on.
            So be a bit more specific with the answer please :)

            To be more clear here is what I want to do:
            var myVariable1:Number;
            var myVariable2:Number;
            var myVariable3:Number;
            var myVariable4:Number;
            ...
            var myVariable100:Number;

            I want to make this depending on 1 number and to be easy to add and remove from them like this (not working):
            var totalVariables:Number = 100;
            var i:Number;
            for (i=1;i<=n;i++) {
            var this["myVariable"+i]:Number;
            }

            I hope u've got my idea...
            • 4. Re: Can I dynamically create variables ?
              Level 7
              Flamebg,

              > kglad, is this going to create the variable myVariable4

              It'll create those variables, yes.

              > And my point is not to give the existing variables a value,
              > but to create these variables, so I can use them in my
              > script later on.

              You betcha. When you're using the array access operator, you don't use
              the "var" keyword; it's just "one of those things."

              > So be a bit more specific with the answer please :)

              It really doesn't get more succinct than the omission of that one word.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 5. Re: Can I dynamically create variables ?
                kglad Adobe Community Professional & MVP
                n should be 100, or n should be totalVariables and omit the data typing and local variable specification (var).

                and you can just declare them without assigning a value, but what's the point? create them when you need them.