Michiel
Michiel

Reputation: 8103

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!

Upvotes: 0

Views: 1486

Answers (1)

Mattias
Mattias

Reputation: 3907

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;
        }

    }

}

Upvotes: 1

Related Questions