View Full Version : public variables into components
SirDuke
08-11-2007, 03:47 PM
Hi.
I have some variables in my application that i use often and in many custom components. An example you be this:
[Bindable]
private var genderTypes:Array = new Array("Male", "Female");
How can i declare this only once (in my main application) so that i don't have to put this code in all the components that will use it?
If i make it public my components can access it and use it but i get errors at compile time that i want to avoid.
Any ideas?:eek:
drkstr
08-11-2007, 08:04 PM
Error when you make it public? There must be something else going on there.
If you just have one pice of data, this is a quick solution:
in main application
public const GATEWAY:String = 'http://localhost/gateway.py';
in component
private const myGateWay:String = Application.application.GATEWAY;
Although if you have a collection of related data it would probably be better to put it in a singleton class:
public class AuthUser {
//singleton instance makes sure there is only one instance of AuthUser
static private var myInstance:AuthUser;
//constructor takes an inaccessable class as parameter to force use
//of getInstance() for signleton use
public function AuthUser( singletonEnforcer:SingletonEnforcer ) { }
public static function getInstance():AuthUser {
if( AuthUser.myInstance == null ) {
AuthUser.myInstance = new AuthUser( new SingletonEnforcer() );
}
return AuthUser.myInstance;
}
}
//publicly inacsessable dummy class used to enforce signleton
class SingletonEnforcer { }
This requires the use of the static method getInstance to instantiate which will return a reference to a "global" object that you can access any where in your program.
Like this:
import mypgk.AuthUser;
private var myCurrentUser:AuthUser = AuthUser.getInstance();
Hope this helps a bit,
...aaron
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.