7 Replies Latest reply on Aug 23, 2006 12:12 AM by jpwrunyan

    Method to get the static class object?

    Daniel Hai
      Wondering if anyone knew how to get the static portion of a class with using 2 methods within flash.utils? I have a bunch of classes that sub-class 1 type of class -- and i want to store static variables within each class to hold information about that subclass. For instance:

      class Whatever {
      public static const somevar:Array = [];
      }

      var def:Object = getDefinitionByName(getQualifiedClassName(new Whatever()));
      trace(def.somevar);

      This gives me the static object -- but it's a tad slow. I've added a method within each of these that returns the top level class info -- but was wondering if there's a more elegant want to do it ...
        • 1. Re: Method to get the static class object?
          jpwrunyan Level 1
          What do you mean by static portion?
          I think if you extend a class that has static variables, the sub-class uses the super-class static variable unless it is overwritten. So I don't see why you need the superclass class reference if that is all you are trying to do....

          correct me if this is way off the mark...
          class A {
          public static var COUNT:int = 1;
          }

          class B extends A {}

          trace (B.COUNT) // should return 1
          • 2. Re: Method to get the static class object?
            Daniel Hai Level 1
            Hey thanks for the reply.

            I'm trying to get the static portion without knowing the type of object it is -- that is, getting a static portion through an instance of a subclass.

            class A {
            public static var COUNT:int = 1;
            }

            class B {
            public static var COUNT:int = 2;
            }

            class C {
            public static var COUNT:int = 2;
            }

            var a = new A();
            var b = new B();
            var c = new C();

            function something(abstract:A) {
            trace(abstract.COUNT); // trace 1,2, or 3 depending on what actual type it is
            }

            the way i'm doing it now is:

            class A {
            public static var COUNT:int = 1;

            public function get definition():Class {
            return A;
            }

            }

            class B {
            public static var COUNT:int = 2;

            public function get definition():Class {
            return B;
            }

            }



            function something(abstract:A) {
            var definition:Object = abstract.definition;
            trace(definition.COUNT); // traces 1,2
            }
            • 3. Re: Method to get the static class object?
              jpwrunyan Level 1
              Hmm... I see... that's an interesting trick if you can pull it off.

              I think the way you are doing it now is the only way you can do it... to be honest, I am surprised it works at all...

              if your way is working faster than flash.utils then I salute you.
              • 4. Re: Method to get the static class object?
                Daniel Hai Level 1
                thanks, but ... it's still so unelegant!

                you would think in flash.utils there would be:

                flash.utils.getDefinition():Class

                =( thanks for your feedback.
                • 5. Re: Method to get the static class object?
                  inlineblue Level 1
                  Just curious...are you actually running into a performance bottleneck using the flash.utils methods? Or do you just not like calling two methods to get the class? If it's the latter, then it's not something to worry about. This is just how reflection is done in AS3.
                  • 6. Re: Method to get the static class object?
                    Daniel Hai Level 1
                    Hey inline -- no real bottleneck -- though it did seem slow. In this particular case performance is extremely important -- and doing it the get definition():Class route is about 90% faster. (10,000 iterations: 80ms for 2 methods, 7ms for get definition)

                    Speed is a big factor to me, but actually, regrettably -- it's the aesthics that drive me nuts :)
                    • 7. Re: Method to get the static class object?
                      jpwrunyan Level 1
                      I doubt this will be of benefit, but I just found that you can obtain the class name from UIComponent:
                      private var myParent:String;
                      if (parent is UIComponent) {
                      myParent = String(UIComponent(parent).className);
                      }
                      if (myParent=="Button") {
                      _lineThickness = 2;
                      } else if (myParent=="ComboBox") {
                      _lineThickness = 3;
                      }
                      obviously, to get static variables you still have to use getDefinitionByName() to get the class... and it has to be a subclass of UIComponent....