Search code examples
global-variablesflashbuilder4

Use global variables in Flash Builder 4


I would like to use variables throughout my whole Flex project (Flash Builder 4). So I can use them as well as in the main application as well as in all the components, services,... What is the best way of doing this?

Thanks in advance!


Solution

  • You can use the Singleton Design Pattern to accomplish this. Define your "global" variables with getters/setters.

    SingletonExample.getInstance().siteWidth = 550;
    

    There are a lot of ways of writing a singleton class, here is one example:

    package
    {
        public final class SingletonExample
        {
    
            private static var _instance : SingletonExample = new SingletonExample();
    
            public function SingletonExample()
            {
                if(_instance)
                    throw new Error( "Singleton and can only be accessed through SingletonExample.getInstance()" ); 
            }
    
            public static function getInstance() : SingletonExample
            {
                return _instance;
            }
    
        }
    
    }