9 Replies Latest reply on Mar 11, 2010 7:32 AM by Florincugir

    Using Variables

    Florincugir

      Hello,

       

      I am trying to do something very simple (I think!) but I cannot figure it out (I am very new in this area....)
      Anyway, inside a script I have two functions; in first function I assign a value to a variable globally defined and I want to use it in the second function, see below:

       

      <mx:Script>
      <![CDATA[

       

      public var var1:String;

       

      private function func1():String
      {
      var1 = 'Y';   // (could be anything, based on some conditions)
      return var1; // 'Y'
      }

       

      private function func2():void
      {
      if (var1 == 'Y') // HERE is the problem: var1 is null!
      {
      // do something
      }
      }
      ]]>
      </mx:Script>

       

       

      This script is used in a FLEX application; it is not the real thing, I've just wanted to point out a situation...

       

      Any help would be welcomed and appreciated...

        • 1. Re: Using Variables
          stickamw

          What is the order you are calling the functions in?  Flex is very Async and if function2 is being called before function1 has time to finish, the value will not have been set. Since function2 has a reliance on data being setup in function1, you may need to have the call to function2 inside and after your assignement in function1.

           

          HTH,

          Adrian

          • 2. Re: Using Variables
            Florincugir Level 1

            Thanks a lot, Adrian, for your answer...

             

            Function1 is called on a creationComplete event for the Application component (basically determines if the call for this application has a parameter; if yes, that variable (var1) is assigned value 'Y')

             

            Function2 is called on a creationComplete event for some components inside the application and, using the variable assigned in Function1, applies (or not!) a blur filter on that component. In my opinion, this should be doable the way I wrote it, but....

            • 3. Re: Using Variables
              stickamw Level 1

              Hi Florin,

               

                   It sounds like a timing issue here...if a component hits creationComplete before the app does, then the var hasn't been set yet.  Whenever I have a scenario like yours, I generally use the initialize"" property on my application vs. a creationComplete.  May want to give that a try.

               

                   Alternatively, instead of relying on the creationComplete for your components, apply your blur in the function1 method.

               

              HTH,

              Adrian

              1 person found this helpful
              • 4. Re: Using Variables
                TanweerHq Level 2

                Hmmmm....

                you are really rocking..

                the order of your function call was wrong..

                first call func1 in app initialize and assign the var =='Y'..

                Then access in func2..

                 

                thats itt..

                1 person found this helpful
                • 5. Re: Using Variables
                  Florincugir Level 1

                  Hi Adrian,

                   

                  I haven't thought a component could hit creationComplete before the application does, but if you say is possible, I believe you (I am new in the area, as I said, not too much experience).

                  Anyway, I used the debugger trying to figure it out and I noticed the variable had the value 'Y' assigned (I put a breakpoint here), but, when I called Function2 (using, also, a breakpoint at beginning) the value 'Y' disappeared... I thought is something wrong with the variable declaration, or something similar...

                   

                  Regarding your suggestion, this is the way I made it work; the problem I had with this method, is I had to call Function1 for every component instead of call it only once, at beginning, and depending of the variable value, apply or not the blur filter for the components...    

                   

                  Thanks a lot for your inputs and all the best,

                   

                  Florin

                  • 6. Re: Using Variables
                    Florincugir Level 1

                    Yes!!! It worked! I've changed the call of Function1 from creationComplete event (for the application) to initialize event and now it is fine

                     

                    Thanks a lot for your input

                    • 7. Re: Using Variables
                      Florincugir Level 1

                      Hi Adrian,

                       

                      It worked, buddy! I've changed the call for Function1 from creationComplete property (for the application) to initialize property, and now it works like a charm...

                       

                      All the best,

                       

                      Florin

                      • 8. Re: Using Variables
                        stickamw Level 1

                        Awesome!  Glad I was able to nudge you in the right direction!  Can you do me a favor and mark the question as answered?

                         

                        Best,

                        Adrian

                        • 9. Re: Using Variables
                          Florincugir Level 1

                          Done. Thanks again, Adrian...

                           

                          Best regards,

                           

                          Florin