8 Replies Latest reply on Dec 20, 2007 6:52 PM by Sreenivas R

    Global Variable...??????


      I need to declare a global variable in Test1.mxml and set the value to use it in Test2.mxml??

      I that possible, and how I can do it?

        • 1. Re: Global Variable...???
          matthew horn Level 3
          If Test1 and Test2 are two components, then I think the only way to do this is to declare the var in the main app that uses these components.
          • 2. Re: Global Variable...???
            Martin-R Level 1
            You can do it one of two ways:

            1. Declare a public variable in Test1, say it's public var test1X. Import mx.core.Application into Test2. If Test1 is the <mx:Application> component, then you can reference the variable by saying Application.application.test1X.

            2. Declare the variable in Test1 like above. Declare a public variable in Test2, say it's public var test2X. When you launch Test2, pass it text1X like this:

            <mycomponents:Test2 text2x = "{this.text1X}" />

            Now Test2's text2X will contain the same value as Test1's text1X.

            • 3. Re: Global Variable...???
              evsPeart Level 1
              Hi Martin,

              Thanks for the answer, but I try with the first one and the global variable always return null the value.

              And with the other solution, the number two, I didnt understand.

              <mycomponents:Test2 text2x = "{this.text1X}" />

              Can you tell me with more detail, please...

              • 4. Re: Global Variable...??????
                Peter Flynn (Adobe) Adobe Employee
                In the first solution, you may be reading the global variable too early or setting it too late.

                The second solution uses data binding, which lets you declare two separate variables in Test1 and Test2 and keep their values in sync.

                If you'll give us some more details on what you're trying to do, we can help you more. When are you setting the variable in Test1? When are you reading it in Test2? Are you also trying to set it from Test2? If so, when? And what are Test1 and Test2 -- is Test1 your application and Test2 is a custom component used inside the application?

                - Peter
                • 5. Re: Global Variable...??????
                  evsPeart Level 1

                  Thanks for the answer.

                  I am trying to do a global variable for use it in an air application, the name Test 1 and Test 2 was just examples name.

                  My Default application is Main.mxml in here I declare the public variable (public var GlobalProjectname:String;), then in Test1.mxml (MXML Component) I set the value (Application.application.GlobalProjectname = txtYearbookName.text;) and I want to use that value in Test2.mxml (the other MXML Component) or another components in the entire application.

                  I hope that you can understand me now much better, but if you have any question jusk ask me about it.

                  Regards and thanks for you time.
                  • 6. Re: Global Variable...??????
                    Peter Flynn (Adobe) Adobe Employee
                    At what point in the program's execution do you set GlobalProjectname in Test1? At what point do you read it in Test2?

                    In any event, I highly recommend learning about data binding. It is the most robust way to share data between different parts of your program. Here is a good place to start: http://www.adobe.com/devnet/flex/quickstart/using_data_binding/

                    Hope that helps,

                    - Peter
                    • 7. Re: Global Variable...??????
                      Hello -

                      This might be a better question for another thread, but it's at least related...

                      Aside from using the "global" variable in the main application, is there any way to pass data to an itemRenderer? Naturally, I mean data other than what comes through via the {data} variable. I need to give the itemRenderer some metadata associated with the entire dataset that gets rendered, and don't want to have to duplicate this information for every element of the set.

                      Thanks. I'll repost in a separate thread if necessary.

                      • 8. Re: Global Variable...??????
                        Sreenivas R Adobe Employee
                        You can use a custom IFactory implementation as a class factory for the itemRenderer and provide it the control. You can have the itemRenderer have a pointer back to the class factory and fetch data from it. You can populate the class factory with what ever data you like.