Skip to main content
Participant
October 1, 2010
Question

Global variables in flex

  • October 1, 2010
  • 2 replies
  • 10845 views

Hi all,

How do we create and use global variables in Flex 4.0.

I tried the following

Created the following on mxml file.

public var gbString:String;

and used the following line in another mxml file.

txtTest.text = FlexGlobals.topLevelApplication.gbString;

While running this file, it shows the following error.

ReferenceError: Error #1069: Property gbString not found on dbtest1 and there is no default value.

at dbtest1/button1_clickHandler()

at dbtest1/___dbtest1_Button1_click()

Please help me

Thanks in advance

Ajay

    This topic has been closed for replies.

    2 replies

    Participant
    October 3, 2010

    Not sure exactly whats going on (im kind of a noob).  But you could declare a bindable public variable in the dbtest1.mxml script block.

    After you do that, you can reference from the Main.mxml file.  Being able to access this variable will allow you to set it to your variable in the Main file.

    PS:  If you do not know bindable syntax it is below.  In a custom component, it allows you to reference the variable from the parent object

    [Bindable]

    var i:int;

    October 3, 2010

    Even I am new in Flex but just want to know ,

    is it not possible to extend or inherit the main mxml and then use the global variable as required.

    October 3, 2010

    OK, such construction workable

    <Application>

    ......

    public var myProperty:String;

    .....

    </Application>

    Then somewhere in project you use the

    <mycomponents:Tester id="tester" />

    or the

    import mycomponents.Tester;

    .....

    var tester = new Tester();

    mxml class named Tester:

    <Group>

         .....

         public function test():void {

              Alert.show(FlexGlobals.topLevelApplication.myProperty)

         }

         .....

    </Group>

    and Then you call to it's function

    tester.test();

    this will show you an alert of Application's myProperty

    October 1, 2010

    Hi Ajay,

    Have you declared the below variable in your main mxml file or any other file..??

    public var gbString:String;

    In order to access the gbString variable using  FlexGlobals.topLevelApplication.gbString you need to declare it main mxml file.

    Thanks,

    Bhasker

    ajaytvpmAuthor
    Participant
    October 1, 2010

    Dear Bhasker

    Thanks for your replay.

    I declared the variable in the main file.

    See the code

    dbtest.mxml

    public var gbString:String;

    protected function button1_clickHandler(event:MouseEvent):void

    {

         FlexGlobals.topLevelApplication.gbString="this is sample";

         navigateToURL(new URLRequest("dbtest1.html"),"_self");

    }

    dbtest1.mxml

    protected function button1_clickHandler(event:MouseEvent):void

    {

         txtTest.text = FlexGlobals.topLevelApplication.gbString;

         // Error shows in this line.

    }

    Regards

    Ajay

    October 1, 2010

    Hi Ajay,

    If you have decalred it in the main mxml file then it should work for you correctly.

    Can you post your total code in the dbtest.mxml ..???

    Thanks,

    Bhasker