4 Replies Latest reply on Aug 22, 2008 8:31 AM by Newsgroup_User

    way to step thru properties of static class?

    squirrelyCode
      Hi,
      I have defined a class with a lot of properties (about 30). There are a number of simple methods I need to write (eg clone, copy values of all properties from one instance to another, reset values of an instance, etc).

      For example

      package {
      [Bindable]
      public class Person{
      public var Var1:int;
      public var Var2:int;
      public var Var3:String;
      ....
      public var VarN:Boolean;

      public static function copyInto(src:Person,dest:Person): void {
      dest.Var1 = src.Var1;
      .....
      dest.VarN = src.VarN;
      }
      }
      }

      Instead of doing lots of assignments. I'd rather do something like
      for each property p in Person
      dest.p = src.p

      I think this can be done for dynamic classes, but what about static classes?

      Thanks!
        • 1. Re: way to step thru properties of static class?
          yigitt
          try this,
          for each(var txt:String in src) {
          dst[txt] = src[txt];
          }

          not 100% sure since I cannot try right now, but should work since each class extends Object.
          • 2. Re: way to step thru properties of static class?
            Level 7

            "squirrelyCode" <webforumsuser@macromedia.com> wrote in message
            news:g8lbbf$5fi$1@forums.macromedia.com...
            > Hi,
            > I have defined a class with a lot of properties (about 30). There are a
            > number
            > of simple methods I need to write (eg clone, copy values of all properties
            > from
            > one instance to another, reset values of an instance, etc).
            >
            > For example
            >
            > package {
            > [Bindable]
            > public class Person{
            > public var Var1:int;
            > public var Var2:int;
            > public var Var3:String;
            > ....
            > public var VarN:Boolean;
            >
            > public static function copyInto(src:Person,dest:Person): void {
            > dest.Var1 = src.Var1;
            > .....
            > dest.VarN = src.VarN;
            > }
            > }
            > }
            >
            > Instead of doing lots of assignments. I'd rather do something like
            > for each property p in Person
            > dest.p = src.p
            >
            > I think this can be done for dynamic classes, but what about static
            > classes?

            Use getDefinitionByName and ClassFactory to dynamically create your class.
            Just be sure that you actually have at least one "hard" reference to each
            class you intend to use, or the class might not get compiled into your swf.
            Here's a full write-up of how to do that:
            http://www.paulofierro.com/archives/520/


            • 3. Re: way to step thru properties of static class?
              levancho Level 3
              you do not need over-cumbersome getDefinitionByName and ClassFactory "stuff"...

              you can do as yigitt suggested, it will work.
              • 4. Re: way to step thru properties of static class?
                Level 7

                "levancho" <webforumsuser@macromedia.com> wrote in message
                news:g8mhu4$re6$1@forums.macromedia.com...
                > you do not need over-cumbersome getDefinitionByName and ClassFactory
                > "stuff"...

                You're right that the ClassFactory is not needed. That was copied and
                pasted from my FAQ, mainly for people who ask a slightly different question.
                But you can't step through a static class as if it were an object. For what
                the op was asking, this may help:

                http://livedocs.adobe.com/flex/3/html/usingas_8.html, particularly
                mx.utils.ObjectUtil.copy() .

                HTH;

                Amy