1 Reply Latest reply on Feb 13, 2009 7:22 PM by Newsgroup_User

    Array operations fail with get function returning Array.

    levancho Level 3
      quote:


      public class A {

      protected function get name () : String { return "A";}
      protected function get myNameArr() : Array { return ["A"]; };

      public function spliceMe() : void{
      myNameArr.splice(myNameArr.indexOf(name),1);
      Alert.show(this.name+": "+myNameArr.length);
      }


      above example does not fail, but item is not removed from Array

      but it works if I do this :
      quote:


      public class A {

      protected function get name () : String { return "A";}

      private var _ar:Array = ["A"];
      public function get myNameArr() : Array { return _ar; };

      public function spliceMe() : void{

      myNameArr.splice(myNameArr.indexOf(name),1);
      Alert.show(this.name+": "+myNameArr.length);

      }





      I guess myNameArr constructs new array on every invocation,.yeah thats fair I guess,

      never mind, This is a "Self Answering Question" :(.

        • 1. Re: Array operations fail silentry with get function.
          Level 7

          "levancho" <webforumsuser@macromedia.com> wrote in message
          news:gn5bkj$qi7$1@forums.macromedia.com...
          >
          quote:


          > public class A {
          >
          > protected function get name () : String { return "A";}
          > protected function get myNameArr() : Array { return ["A"]; };
          >
          > public function spliceMe() : void{
          > myNameArr.splice(myNameArr.indexOf(name),1);
          > Alert.show(this.name+": "+myNameArr.length);
          > }
          >

          > above example does not fail, but item is not removed from Array

          Uh, no, because you've hard coded it to always return ["A"]. Duh.

          > but it works if I do this :
          >
          quote:


          > public class A {
          >
          > protected function get name () : String { return "A";}
          >
          > private var _ar:Array = ["A"];
          > public function get myNameArr() : Array { return _ar; };
          >
          > public function spliceMe() : void{
          >
          > myNameArr.splice(myNameArr.indexOf(name),1);
          > Alert.show(this.name+": "+myNameArr.length);
          >
          > }
          >

          >
          > that's an obvious bug, is not it?

          With your code? Yes.

          > P.S : I am pretty sure all other array operations do not work either

          Absolutely. If you have a function that is always set to return a single
          hard coded value, there are no operations you can perform on it to ever make
          it return any other value.