4 Replies Latest reply on Jan 5, 2007 9:48 AM by FlightGuy

    Global Objects/Variables

    Jfill
      I have looked all over this forum and in other places and cannot find the answer to this question. Is it possible to set an instance of an object or a variable to something to the equivalent of the "Application scope" in ColdFusion. I need to be able to access some variables in several MXML files and do not really want to have to create separate events for each type I need to access.

      Is this possible? I am not talking about not using events at all for data binding ect. I am speaking in terms of some variables that will be set right when the application instantiates and will not change after that.

      Any ideas would be helpful! Thanks!


        • 1. Re: Global Objects/Variables
          FlightGuy Level 1
          Try defining a class (AS or MXML), and then define static properties. These can then be accessed from anywhere, anytime. For example:

          public class MySettings {
          public static var age:int = 34;
          public static var name:String;
          }

          Then anywhere in your application,
          trace(MySettings.age);

          or
          MySettings.name = "Tim";
          trace(MySettings.name);

          Depending on what you're using it for, you may choose rather to use the GoF singleton pattern:

          public class Settings {
          private var singleton:Settings;
          public function get instance():Settings{
          if (!singleton) singleton = new Settings();
          return singleton;
          }

          Then make all your settings instance properties of the Settings class. Now when you want to use it, you can say

          Settings.instance.name

          or

          private var mySettings:Settings = Settings.instance;
          trace(mySettings.name);
          • 2. Re: Global Objects/Variables
            ntsiii Level 3
            Also, Application.application will reference the top, application-level scope, giving you access to anything declared public at that level. You may need to import mx.core.Application;

            In flightGuy's example, if your MySettings class is not in the same folder as the consuming code, you need to import it as well.

            I prefer the static property approach myself. Static "settings" or "utils" just feels cleaner than a bunch of stuff in a script block in the application.

            Tracy

            Tracy
            • 3. Re: Global Objects/Variables
              Jfill Level 1
              Thanks for the help, that is great. I have messed Flights first example and I think I am going to run with that. Using a class with static vars. I agree Tracy, it does seem cleaner to use that approach rather than referencing Application.application.....all over the place, but is nice to know that is possible.

              Once again thanks a bunch for the tips!

              • 4. Re: Global Objects/Variables
                FlightGuy Level 1
                The other advantage of using static properties rather than putting public properties into your application is that you get compile-time type checking - quite aside of the convenience of having auto-completion in FlexBuilder.

                I'm not sure if you can make static properties bindable - I haven't tried. If not, and you need to bind to these properties, use the singleton pattern described above. This will give you binding and strong typing.

                Tim