5 Replies Latest reply on Apr 3, 2007 6:20 AM by Cheree Singal

    for-in loop

    Cheree Singal Level 1
      Hi all,

      I have a small question abou for-in loop.

      Suppose there a class defined as:

      public class TestClass{
      public var a: Number;
      public var b: Number;
      }

      and I give

      var testObj: TestClass = new TestClass();

      for(var j: string in testObj){
      Alert.show(j);
      }

      On the other hand, I have:

      var testObj: Object = new Object();
      testObj.a=1;
      testObj.b=1;

      for(var j: string in testObj){
      Alert.show(j);
      }


      In the first case, the for loop doesnt work whereas it works in second one!
      Does that mean that for-in loop works only in case of Object and not well-defined class instance.

      Thanks in advance,
      Cheree
        • 1. Re: for-in loop
          ntsiii Level 3
          I believe that you class has to be declared as "dynamic" . Now, I do not recall any details, or the exact syntax to set that. Search around, if you can't find it i'll look further.

          Tracy
          • 2. Re: for-in loop
            BillHeit2
            It looks like in your first case you are not setting the values and the second case you are. Try:

            public class TestClass{
            public var a: Number = 1;
            public var b: Number = 1;
            }
            • 3. Re: for-in loop
              mike_morearty Level 1
              Tracy is halfway there. for loops only find dynamic members. Just declaring the class to be dynamic is a necessary first step to being able to assign dynamic members to it, but the "for" loop will still only find the dynamic members, not the ones that were defined at compile-time.

              Here is an example. In this code, 'i' was defined at compile time, 'j' at runtime:

              package {
              import flash.display.Sprite;

              public dynamic class forloop extends Sprite
              {
              public var i:int;

              public function forloop()
              {
              this.i = 3;
              this.j = 4;
              for (var z:* in this)
              {
              trace(z);
              }
              }
              }
              }

              The output of this program is just 'j'.

              Yes this seems quite weird, and definitely takes getting used to. It is partly a result, I think, of the fact that ActionScript/ECMAScript started as an entirely dynamic language, but as of AS3/ECMAScript4, it also has compile-time type safety; the blend of the two capabilities can get quite odd sometimes.
              • 4. Re: for-in loop
                ntsiii Level 3
                Thanks Mike.

                Cheree, exactly what are you trying to do with the for in loop? Maybe we can help with a solution.

                By the way, you can also "introspect" class/objects using "describeType". It returns xml with a lot of info. Here is an example with some detail.

                http://www.cflex.net/showfiledetails.cfm?ChannelID=1&Object=File&objectID=580

                Tracy
                • 5. Re: for-in loop
                  Cheree Singal Level 1
                  Thanks every one for your replies! I am so sorry that I could not reply as I was on a leave.
                  Tracy, I have a well defined object (not a dynamic one), all the attributes of which are of data type Boolean. I want to get the values of each of the fields of the object. I thot I could do it using a for-in loop but as is evident from all the replies, I cant do it :-(

                  Thanks a lot,
                  Cheree