Van De Velde: I tried you example to get the singleton pattern down of creating some global static variables. Of course I created my package and class for my own set-up and I'm getting compile errors.
Thanks for your help, being a relative newcomer, I appreciate your time and patience, Patrick com.yourProject.config.AppSettings.getInstance().getWhatever(); (of course I renamed this to package, etc...) Severity Description Resource In Folder Location Creation Time Id 2 1120: Access of undefined property com. p1.mxml polls line 57 November 18, 2006 10:12:59 PM 4099 var myAppSettings:AppSettings = com.yourProject.config.AppSettings.getInstance(); myAppSettings.getWhatever(); Severity Description Resource In Folder Location Creation Time Id 2 1046: Type was not found or was not a compile-time constant: AppSettings. p1.mxml polls line 56 November 18, 2006 10:14:26 PM 4102 Van De Velde Hans wrote: > > A good practice to keep global variables is by creating a class with > *public static variables* > and *access those properties *directly *via the class*, for example > via com.yourProject.Globals.globalParam1, > > or if you want something more advanced, create a singleton class : > like for example : > > package com.yourProject.config > { > public class AppSettings > { > // self-reference > private static var appSettings:AppSettings; > > public var _whateverVariable:String; > > // please note the private constructor > private function AppSettings(){ > } > > public static function getInstance():AppSettings > { > if(!appSettings){ > appSettings = new AppSettings(); > } > > return appSettings; > } > > public function getWhatever():String > { > return "whatever"; > } > > // implicit getter > public function get whatever():String > { > return _whateverVariable; > } > > // implicit setter > public function set whatever(_whateverVariable:String):void > { > this._whateverVariable = _whateverVariable; > } > } > } > > //////////////////////////// > > then, use it like this: > > com.yourProject.config.AppSettings.getInstance().getWhatever(); > > or directly access a property: > > com.yourProject.config.AppSettings.getInstance().whatever > > to simplify the reading of your code, you can also store the returned > instance in a local variablen like this: > > var myAppSettings:AppSettings = > com.yourProject.config.AppSettings.getInstance(); > myAppSettings.getWhatever(); > > > > > > -----Original Message----- > *From:* flexcoders@yahoogroups.com > [mailto:[EMAIL PROTECTED] *On Behalf Of *Gordon Smith > *Sent:* zaterdag 11 november 2006 6:29 > *To:* flexcoders@yahoogroups.com > *Subject:* RE: [flexcoders] OK, seriously. Why can't I make a > global var? > > <pedantry>This is not technically a global variable. It is simply > an instance var of the Application object.</pedantry>. > > - Gordon > > ------------------------------------------------------------------------ > > *From:* flexcoders@yahoogroups.com > [mailto:[EMAIL PROTECTED] *On Behalf Of *Karl Johnson > *Sent:* Friday, November 10, 2006 12:57 PM > *To:* flexcoders@yahoogroups.com > *Subject:* RE: [flexcoders] OK, seriously. Why can't I make a > global var? > > You can use mx.core.Application.application.myVar = "hooray!"; if > you really want to access a global variable like that. > > |< > > Karl Johnson > > Cynergy Systems > > ------------------------------------------------------------------------ > > *From:* flexcoders@yahoogroups.com > [mailto:[EMAIL PROTECTED] *On Behalf Of *poolpcs > *Sent:* Friday, November 10, 2006 1:13 PM > *To:* flexcoders@yahoogroups.com > *Subject:* [flexcoders] OK, seriously. Why can't I make a global var? > > After years (and years and years) of programming Flash, I'm used > to being able to simply say: > > _root.myVar = "hooray!" > > and being able to see that variable anywhere. > > Why in God's green earth can't I do the same in Flex? I have a > custom login component which > is currently a child of the base app, sitting in a view state. All > I want to do is grab one of the > returned values that the login gets (emailaddress) and let other > sections of my program see > that. > > I cannot for the life of me figure this out. I see stuff about > making static members of classes, > referencing "parentApplication" etc. > > I am in no way a newbie to interactive development, and this is > just insane. Within my first > two days of using Flex I figured out how to create a complete > login/registration system that > talks to a DB, and figured out how to use the socket class to > directly talk to my mail server > and format a proper SMTP message, but I can't do this. > > Clear guidance would be appreciated before i pitch my G5 out the > window. > >