9 Replies Latest reply on Jun 12, 2006 7:31 AM by kglad

    Scope of variable in event handler

    Level 7
      function showAlert(num):Boolean {
      var ret:Boolean;
      var myClickHandler = function (evt) {
      if (evt.detail == mx.controls.Alert.OK){
      ret = true;
      } else {
      ret = false;
      }
      }
      alert("Only " + num + " of this item are available. Click OK to buy
      these.", "Availability", mx.controls.Alert.OK |
      mx.controls.Alert.CANCEL, myClickHandler);
      return ret;
      }


      if (showAlert(totavail)) {//etc.

      Why is it that the value of ret set in the event handler is not the same
      as ret in the main function which remains undefined?

      What is the correct way to write this??

      Doug
        • 1. Re: Scope of variable in event handler
          kglad Adobe Community Professional & MVP
          when you prefix a variable declaration with var, you're making that variable local within the context of its definition. in your situation ret (and myClickHandler) are only defined and accessible from within showAlert().

          to remedy, do not prefix with var. (and there's no need to type your variables.)
          • 2. Re: Scope of variable in event handler
            Level 7
            If I remove var, I get the error:

            There is no property with the name 'ret'.

            Doug
            • 3. Re: Scope of variable in event handler
              kglad Adobe Community Professional & MVP
              again, datatyping your variables is unnecessary.
              • 4. Re: Scope of variable in event handler
                Level 7
                If I remove the datatyping, there is no change. I still get the errors:

                There is no property with the name 'ret'.

                There is no property with the name 'myClickHandler'.

                Doug
                • 5. Re: Scope of variable in event handler
                  Level 7
                  I have found that the following code works:

                  function showAlert(num) {
                  var ret;
                  var myClickHandler = function(evt) {
                  var ret2;
                  if (evt.detail == mx.controls.Alert.OK) {
                  ret2 = true;
                  } else {
                  ret2 = false;
                  }
                  return ret2;
                  }
                  ret = alert("Only " + num + " of this item are available. Click OK to
                  buy these.", "Availability", mx.controls.Alert.OK |
                  mx.controls.Alert.CANCEL, myClickHandler);
                  return ret;
                  }

                  But I still don't understand why my original code didn't work. When I
                  'var ret;' in the main function, the ret in the handler does not error.
                  But when I don't 'var ret;' in the main function I get errors for every
                  line where ret is occurs even those in the handler.

                  So if var makes ret local to the main function why don't the lines with
                  ret in the handler throw an error?

                  Since they don't, they surely must be the same variable, whilst at the
                  same time they're obviously not the same as it doesn't work.

                  This seems totally illogical to me and I really can't understand it.

                  Doug
                  • 6. Re: Scope of variable in event handler
                    kglad Adobe Community Professional & MVP
                    the code below should generate no compiler errors. i don't know if it does what you want because i don't know what you're trying to do.

                    your code appears convoluted, at best. why do you define the function myClickHandler within another function showAlert()?

                    • 7. Re: Scope of variable in event handler
                      kglad Adobe Community Professional & MVP
                      actually, now that i look at my interpretation of your coding, it doesn't make sense. showAlert() will return undefined when it's first executed. that surely isn't what you want. therefore, it makes no sense to have myClickHandler defined within showAlert().
                      • 8. Re: Scope of variable in event handler
                        Level 7
                        All I want to do is run showAlert if the number of this item in stock
                        now is less than the quantity requested by the customer. showAlert then
                        simply allows the customer to choose to take what is available or give
                        it up.

                        Whether, the handler is inside or outside the main function won't alter
                        the code as far as I can see, so I shall leave it as it is since the
                        present code has the unrivaled virtue that it works.

                        But I admit, it seems an ugly solution. Can anyone come up with a better
                        one?

                        And I still don't understand why the scope in the handler is separate
                        when var ret; is set in the main handler, and not separate when it isn't!!

                        Doug
                        • 9. Re: Scope of variable in event handler
                          kglad Adobe Community Professional & MVP
                          if myClickHandler executes after the customer selects an item and updates your variable ret and you want to make ret a local variable, you can use the following code. notice that the variable ret is local to myClickHandler. therefore, for showAlert() to have access to this variable it is passed as a function parameter.

                          if you don't want to pass ret as a function parameter, then you must remove the variable declaration (var ret etc...) in myClickHandler that makes it local to myClickHandler. then if the functions are on the same timeline showAlert has access to ret as soon as myClickHandler defines it.