4 Replies Latest reply on Feb 8, 2008 1:06 PM by clbeech

    Using for( x in obj )

    ooba

      If I have this in my Flash file all is great, but I want to put this into a custom Class file. What flash class do I need to import into my custom class to get this to work.

      var x = 0;
      for( item in param_ ){
      x=x+1;
      trace( x + ': ' + item + ' = ' + param_[item] );
      } // for

      The error message in the Output box is "There is no property with the name 'item'."
      Again this only occurs if I place the code in the Class file I have made.
        • 1. Re: Using for( x in obj )
          clbeech Level 3
          couple of things here, first you should not use 'x' as a variable name, because it is a reserved word in Flash, used as a standard Object property. Second you should add 'var' to the loop condition as in:

          for(var item in param_) { ... }

          third, side note, you can increment a variable's value using x++; instead of x=x+1; (although you should not use x).
          • 2. Re: Using for( x in obj )
            Level 7
            ooba,

            > The error message in the Output box is "There is no property
            > with the name 'item'."

            The reason you're seeing that is because you haven't declared your
            "item" variable anywhere. Historically, Flash has been very lenient (such
            as not requiring variable declarations), but in class files, you have have
            to start sitting up straight, so to speak. ;)

            var x:Number = 0;
            for (var item:String in param_ ) {
            x ++;
            trace(x + ": " + item + " = " + param_[item]);
            }

            I added the var keyword in front of item; that should do it. I also
            added strong typing (:Number, :String) to your variables, which in
            ActionScript 2.0 helps with code completion and useful error warnings at
            compile time. Finally, I swapped your expression x = x+1 with x++, just
            because the ++ operator makes a nice shortcut.


            David Stiller
            Co-author, Foundation Flash CS3 for Designers
            http://tinyurl.com/2k29mj
            "Luck is the residue of good design."


            • 3. Re: Using for( x in obj )
              ooba Level 1
              Thank you to both of you. i had found after reading clbeech response the issue with declaring the var and decided to add in strong type as well. Being that David Stiller answer was more descriptive to a reader I marked his as the answer. Thanks again fellow Flashers.
              • 4. Re: Using for( x in obj )
                clbeech Level 3
                lol!! - you're welcome ooba, and np! David's answers will always be much clearer than mine LOL!! :)