Ultrashock Forums > Flash > ActionScript
global class properties

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
global class properties
Old 2008-02-22

Hello,

I want to set some global variables across my application. However I have read that using _global isn't good practice so I have tired setting them as properties within a class. Is this right and how do I access them in other classes and movies?

Code:
class Startup
{
public static var URL:String = "http:www.google.com"
public static var IMAGE_URL:String = "http://images"
//
public function doStartup()
{
}
postbit arrow 7 comments | 223 views postbit arrow Reply: with Quote   
Registered User
bob_300 is offline
seperator
Posts: 71
2008-01-20
seperator

Ultrashock Member Comments:
bob_300 bob_300 is offline 2008-02-22 #2 Old  
I got it to work by importing the Startup class to the other class that need it, and then assiging it within the new class;
Code:
private var IMAGE_URL = doStartup.IMAGE_URL;
Is this correct?
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-02-22 #3 Old  
Couldn't you just do it this way which is maybe a little bit cleaner.

ActionScript Code:
  1. class Startup
  2. {
  3.     private static var URL:String = "http://www.google.com";
  4.     private static var IMAGE_URL:String = "http://images";
  5.    
  6.     public function doStartup()
  7.     {
  8.     }
  9.    
  10.     public static function get url():String // read only
  11.     {
  12.         return URL;
  13.     }
  14. }

ActionScript Code:
  1. import Startup;
  2.  
  3. trace( Startup.url );
Reply With Quote  
bob_300 bob_300 is offline 2008-02-22 #4 Old  
If I set the properties as private, flash won't let me access them. But using your code for multiple properties I have this...

Is this correct?

Code:
class Startup
{
	
public static var WSDL_URL:Object = "http://www.google.com"
public static var IMAGE_URL:Object = "http://images/"
public static var uId:Object = "user"
public static var pwd:Object = "password"
//
public function Startup()
{
}
	public static function get _WSDL_URL():Object // read only
    {
       return WSDL_URL;
	
    }
	//
	public static function get _IMAGE_URL():Object // read only
    {
        return IMAGE_URL;
	
    }
	//
	public static function get _uId():Object // read only
    {
       return uId;
	
    }
	//
	public static function get _pwd():Object // read only
    {
       return pwd;
	
    }
}
Code:
private var IMAGE_URL = doStartup._IMAGE_URL;
	private var WSDL_URL = doStartup._WSDL_URL;
	private  var uId = doStartup._uId;
	private  var pwd = doStartup._pwd;
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-02-22 #5 Old  
You can set your variables as private and use a getter to access the variable. If you were trying to access WSDL_URL directly like "Startup.WSDL_URL" then yes it would have to be public. But if you use a getter function like:

ActionScript Code:
  1. public static function get _WSDL_URL():Object
  2. {
  3.     return WSDL_URL;
  4. }


Then you can make the variable private since your not accessing it directly. But the getter would have to public since your trying to access it from another class.
Reply With Quote  
bob_300 bob_300 is offline 2008-02-22 #6 Old  
Thanks, is there any benefit making it private?
Reply With Quote  
Isocase's Avatar Isocase Isocase is offline Isocase lives in United States 2008-02-22 #7 Old  
Hopefully I can explain myself correctly here. By making it private and using a getter to access that its read only which means you can't change the variable value, you can only read it.

Here is a little example that I came up with and hopefully its right. Anyone please feel free to correct me if I am wrong

ActionScript Code:
  1. class Foo
  2. {
  3.     // url can be accessed outside of the class and modified
  4.     public static var url:String = "http://www.google.com";
  5.    
  6.     // url1 is private so its only accessible inside the class
  7.     private static var url1:String = "http://www.ultrashock.com";
  8.    
  9.     public function Foo()
  10.     {
  11.     }
  12.    
  13.     // By using a getter you can read it only but you cannot modify the value
  14.     public static function get ultrashock():String //read only
  15.     {
  16.         return url1;
  17.     }
  18. }

ActionScript Code:
  1. //Traces out initial value of url
  2. trace( Foo.url );
  3. //Traces out the get method of url1
  4. trace( Foo.ultrashock );
  5. trace( "" );
  6.  
  7. // Here I can modify the value of url
  8. Foo.url = "http://www.yahoo.com";
  9. //Now the value is something different then what you originally had
  10. trace( Foo.url );
Reply With Quote  
Codemonkey's Avatar Codemonkey Codemonkey is online now Super Moderator Codemonkey lives in Netherlands 2008-02-22 #8 Old  
Hi Bob, Isocase is right. Generally you would just make the vars public static *and* final/constant so that it by nature would be read only and thus safe to use without getter. AS2 doesn't support this keyword, so the constant is effectively mimiced by using a static getter function with the same name:

ActionScript Code:
  1. static class ConstantVars {
  2.   public static function get MYCONSTANT() { return "this is a constant value"; }
  3. }
ActionScript Code:
  1. trace(ConstantVars.MYCONSTANT);
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: