4 Replies Latest reply on Jul 27, 2008 1:12 AM by jonnyballe

    access the application class methods

    jonnyballe
      I am quite new to Flex and actionscript 3 and what I am trying to figure out is how to
      access public methods, getters and setters in the main Application.mxml file from an external class.
      In my test files below I know how to instanciate the Getter.as class and use the getters, methods and so on but what if I would like to do the opposite and access the actionscript methods, getters and setters in the Main.mxml from the Getter.as? How do I do this?

      Thanks

      Main.mxml--------------------------------------------------------------------------------- -----------------------------------------------------------
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationCompleteHandler();">

      <mx:Script>
      <![CDATA[
      import flash.display.Sprite;
      import Getter;

      private var _baseName:String = "default basename";
      private var _Getter:Getter;

      private function creationCompleteHandler():void
      {
      _Getter = new Getter();
      trace(_Getter.aVar);
      _Getter.aVar = "this is a new variable";
      trace(_Getter.aVar);
      }



      //How do I access these from the Getter class------------------------------------
      public function tester():void{
      trace("tester");
      }


      public function set baseName(n:String):void{
      _baseName = n;
      trace("Setting _currentSong to:" + _baseName);

      }

      public function get baseName():String{
      return _baseName;
      trace("Getting currentsong:" + _baseName);
      }
      ]]>
      </mx:Script>
      </mx:Application>
      end Main.mxml-------------------------------------------------------------------------------- -----------------------------------------------------

      Getter.as--------------------------------------------------------------------------------- ------------------------------------------------------------
      package
      {

      public class Getter
      {
      private var _aVar:String = "default var";

      public function Getter()
      {
      trace("Getter hello");
      //Here I would like to access the Main.mxml getters and setters and public methods
      }


      public function set aVar(p:String):void{
      _aVar = p;
      trace("Setting _aVar to:" + _aVar);

      }

      public function get aVar():String{
      return _aVar;
      trace("Getting currentsong:" + _aVar);
      }

      }
      }
        • 1. access the application class methods
          VarioPegged Level 2
          Your Getter class seems to act as a value object. First thing I would suggest is to adhere to the basic principles of OOP, encapsulation being the main principle that would apply here. You don't want Getter.as to be able to call a method on the application directly if you're at all interested to reuse Getter.as somewhere else, so here's what I'd suggest.
          1. Have the main application implement a custom interface that defines the methods you want to call from Getter.as
          2. Pass the Getter class the instance of the application (cast as the interface type) to the Getter.as constructor
          3. Call the functions from the Getter class

          OK, now that you know how to do it the right way, here's the shortcut 'wrong' way:
          You could call Application.application.whateverMethod() directly from the Getter class, but I'd strongly discourage using this type of coding practice.

          TS
          • 2. Re: access the application class methods
            Level 7

            "VarioPegged" <webforumsuser@macromedia.com> wrote in message
            news:g6fs0e$pqb$1@forums.macromedia.com...
            > Your Getter class seems to act as a value object. First thing I would
            > suggest
            > is to adhere to the basic principles of OOP, encapsulation being the main
            > principle that would apply here. You don't want Getter.as to be able to
            > call a
            > method on the application directly if you're at all interested to reuse
            > Getter.as somewhere else, so here's what I'd suggest.
            > 1. Have the main application implement a custom interface that defines the
            > methods you want to call from Getter.as
            > 2. Pass the Getter class the instance of the application (cast as the
            > interface type) to the Getter.as constructor
            > 3. Call the functions from the Getter class
            >
            > OK, now that you know how to do it the right way, here's the shortcut
            > 'wrong'
            > way:

            I think that's arguable. Some people would say this also breaks
            encapsulation, and that the "right" way is to broadcast an event from the
            class that the application will listen for and react to.


            • 3. Re: access the application class methods
              VarioPegged Level 2
              :-) I favor event dispatching over all, as many of my posts on the topic within this forum show. There are definitely levels of encapsulation, though. If you must call a method of another class from within your class, this is what I'd suggest with code reuse in mind, although there's obviously a dependency that's been created through passing the app instance typed as the interface to the constructor, but not nearly as inflexible as passing a reference of the application instance itself or using Application.application.

              TS
              • 4. Re: access the application class methods
                jonnyballe Level 1
                Thanks for all the good answers =)