2 Replies Latest reply on May 14, 2008 5:25 PM by ntsiii

    Passing variables between functions

    pope on acid Level 1
      I want to keep a variables value to use in filter function,but this way it doesn't works:

      [Bindable]
      public var knop:String = new String;

      [Bindable]
      public var selKnop:String = new String;

      //<---------------------------------------------------------------------------->
      public function weekFilter(selKnop:String):void
      {
      var knop:String = selKnop;
      sailings.filterFunction = filterBysWeek;
      sailings.refresh();
      }

      public function filterBysWeek(item:Object):Boolean
      {
      var isMatch:Boolean = false
      if(item.sweek.toLowerCase().search(knop) == -1)
      {
      isMatch = true
      }
      return isMatch;
      }
        • 1. Re: Passing variables between functions
          VarioPegged Level 2
          I'm assuming you haven't assigned any values to the bindable public vars knop and selKnop by the time you invoke the weekFilter() method. If this is the case, the scope of your variables is the problem.

          Your declaration of the variable "knop" in weekFilter() is local in scope. Referring to it in filterBysWeek() before it's been assigned a value therefore wouldn't work. If you're looking to set the public var knop in weekFilter(), then you'd do this:

          public function weekFilter(selKnop:String):void
          {
          knop = selKnop;
          sailings.filterFunction = filterBysWeek;
          sailings.refresh();
          }

          Note that the parameter selKnop you have in the local scope of weekFilter() is NOT referring to the bindable public var "selKnop" you have declared at the top of your code. But you probably knew this, right?

          TS
          • 2. Re: Passing variables between functions
            ntsiii Level 3
            1) Do not use "new" to create a string variable.
            public var knop:String;

            2) Do not re-declare the variable inside the weekFilter function. That makes a local var, whose scope is only within the function.
            knop = selKnop;

            3) Do not use [Bindable] unless you need to bind to the var.

            Tracy