Sunday, December 21, 2008

Global Variables in Flex Apps

Global variables are useful in numerous occasions in our application. So here it is how you do it in flex.

Here is how you do it..

Step 1: Import Application core package

       import mx.core.Application


Step 2:  Declare the variable in the main application (in our case it should be Indemand_Video.mxml)


  public var gVariable:String = "gVariable";


Step 3: Use the variable "gVariable" anywhere in the application as follows


Application.application.gVariable


I ran a test to access this global variable in chat.mxml. It worked fine.


I think we can use the same application object to access any public function in the Indemand_Video.mxml main application.. Is it strinking any bells ?


Update:


1) I created public alertTest():void function in indemand_video.mxml.

2) called the function in chat.mxml as Application.application.alertTest();

  Bingo it worked.....




No comments: