Global Variables within Flex Applications

The question of how to set up global variables in a Flex application came up on the Flex Components list recently, and as it comes up every so often, I figured I would blog one of the answers.

This is actually very easy. Just define a public variable within your main application class, like so:

[code][/code]You can then access that variable from anywhere within the Flex application via:[code][/code]If you need to, you can still make the variable bindable.Post and questions or suggestions in the comments.

12 Responses to Global Variables within Flex Applications

  1. Paul Andrews says:

    But how many situations are there where their use is really appropriate? Should we really be building applications that depend on global variables?That said, I can see some justification in terms of having access to the application environment and perhaps such things as login and security credentials, but I wonder how many use global variables at the expense of creating a maintainable application?I’d be interested in knowing good uses of global variables!Paul

  2. mike chambers says:

    Well, that is up to developer. However, as I have begun to work on Apollo applications and once again have more user defined settings (which must be persisted between sessions), I have found my self using this.I will sometimes store a settings class as a global variable and allow other parts of the application to bind to individual settings.I find this becomes more useful as my application becomes more complex, and keeps me from having to pass the settings class around to everything in my app that may need it.mike

  3. Andrew Trice says:

    Another thing you can do to create a global variable is create a singleton class, and refer to the variable instances within the singleton. Depends how you use it, but could be useful and cleaner in various scenarios, although it is definitely not as simple as what you mentioned above. For instance, your data model can be declared (similar to cairngorm) as:var model : ModelLocator = ModelLocator.getInstance();You can always access the model anywhere in your application, at any time using this method. An update to the model in one component would be an update to the “global” variable, and the change would be available throughout your entire application.

  4. Spike says:

    I’m surprised you don’t advocate using a singleton instead.Using doesn’t give you compile time type safety unless you cast the application instance to the file name of your main app entry point. Casting to an application specific class makes the consuming component harder to maintain and use across multiple apps.I appreciate that is simpler to understand, but in the long term it probably isn’t as robust as the singleton approach.

  5. Suketu Vyas says:

    Hi mike,I think it may be not appropriate to call this as global variables as you can access any public or private variable or function (which is declared in the application) using Application.application.In my opinion Application.application will be something similar to _root in AS2 using which we can target the root of the application and call any thing which is declared on the root. In the case of flex application root will be the Application.application so using that you can access anything that is declared in Application.Global variable in AS2 has a special treatment; you have to specify _global to declare the global variable that can be called from anywhere form the application.Any Thoughts?~ Suketu Vyas

  6. mike chambers says:

    –I think it may be not appropriate to call this as global variables as you can access any public or private variable or function (which is declared in the application) using Application.application.–I dont necessarily agree with that. Regardless though, the question was how to access a variable gloabally from within the application.This example, as well as a Singleton example pointed out in the comments both address that.mike

  7. Dave says:

    Just starting with Flex and AS, could someone point me to a more complete example of using a singleton to hold vars.Thanks, Dave

  8. Dave, below is an example of a Singleton Class in it’s most basic form:package {public class Example {private static var instance:Example;public function Example(access:Access){if (access != null) {if (Example.instance == null){Example.instance = this;}}else {throw new Error(“Illegal operation”);}}//provides access to singleton instancepublic static function getInstance():Example{if (instance == null){instance = new Example();}return Example;}}}//restricts constructor access to privateclass Access {}

  9. zwetan says:

    but you can access the global object tooand declare it there then ;)———-package {import flash.display.Sprite;import flash.utils.trace ;public class MainClass extends Sprite{public function MainClass() {trace(“>> _global : ” + _global) ;trace(“>> myGlobalProperty : ” +_global.myGlobalProperty) ;}}}if (_global == null ) {var _global = this ;}———-you gotta love the internal namespace =)

  10. Phillip Kerman says:

    I’d think singleton is appropriate for when you want a “global-like” class (and, naturally, only one instance) but not necessary for when you just want variables. In fact, I’d do something similar to this but rather keep the variable as a public var in a class accessible to all parts of your app. Usually, I’ll have one class Constants containing nothing but public static vars. From anywhere I access to set or get them using Constants.VAR_NAME. For all I know I’m doing this backwards but it has solved the use-case that _global used to provide.

  11. Simon says:

    Reading the above comments with interest. Why store the vars in a Singleton that you need to instaciate.Could you not build a class that contains only static methods and pass the settings in via a static method. The class would then offer access to the settings via its static methods. There would then be no need for classes to ‘know’ about the instance, only the class.

  12. Theo says:

    I realize that this will appear very rude, but I must say that I am shocked to see you Mike, a senior product manager at Adobe, give out tips on how to create global variables. I really hope that the developers on your team don’t take tips from you.I do apologize if I have misunderstood your intentions on this, and I do understand that this is a “feature” often asked for. But really, should you be promoting bad design?And to those of you suggesting singletons and monostates, while your intents are probably good too, the solutions you provide are no less global variables than Mike’s, they are just better disguised.A properly designed application does not need global variables, nor does it need singletons to provide global access to resources. That is not to say that the singleton pattern is bad, but it’s shouldn’t be used to solve the “problem” of lack of global variables.You can read more about proper use of the singleton pattern at the Portland Pattern Repository: where there is also a discussion on global variables: