9 Replies Latest reply on Nov 27, 2006 2:50 PM by Newsgroup_User

    Array in object doesn't duplicate upon instantiating

    Jean-P. Level 1
      I guess I'm victim of my own misunderstanding of OOP but can somebody tell me why if I create two instances of an object which itself creates another array object, I end up referencing the same array ?

      I've attached some oversimplified code to make things clearer. Why are both arrays containing the same data ? Aren't they different objects ?


        • 1. Array in object doesn't duplicate upon instantiating
          anonymous thing
          the Array class is static when your property is initialize outside of your constructor or not in a method... to solve your problem try to initialize your array in your constructor

          • 2. Re: Array in object doesn't duplicate upon instantiating
            Jean-P. Level 1
            Thanks a lot. I did not know that.

            I've initialized variables in this way for quite some time but never ran into the problem because they all were single instance objects. I'll have to clean up some of my old code.

            I tried to google this issue but couldn't figure out what to search for... Thanks again.
            • 3. Re: Array in object doesn't duplicate upon instantiating
              anonymous thing Level 1
              You must know that this behavior only happen with the Array class. For everything else you can continue like you did before.
              So this examples work perfectly...

              public var strTest:String = "";
              public var objTest:Object = new Object();
              //.....
              • 4. Re: Array in object doesn't duplicate upon instantiating
                Jean-P. Level 1
                That's odd. Is there a reason why this would only happen with the array class ? An array is an object like any other, no ? Is this the way other OO programming languages work ?

                Many questions... If you do not have time, don't bother. I'm just curious and you already solved my problem. Thanks again.
                • 5. Re: Array in object doesn't duplicate upon instantiating
                  anonymous thing Level 1
                  Honestly, I don't know!
                  Like you, I searched and found nothing that could explain this behavior... check this livedoc link : http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001191.html
                  at the "Francis Cheng said on Jan 18, 2005 at 9:24 AM :" comment.

                  If you find a clue it would be nice if you can post it...
                  • 6. Re: Array in object doesn't duplicate upon instantiating
                    Level 7
                    > You must know that this behavior only happen with the Array class.
                    No .. it happens on all object reference classes (ie thing sother than
                    primitive types like numbers booleans and strings)

                    > For
                    > everything else you can continue like you did before.
                    > So this examples work perfectly...
                    >
                    > public var strTest:String = "";

                    ... That's fine

                    > public var objTest:Object = new Object();

                    ... but that will have the same probelms
                    --
                    Jeckyl


                    • 7. Re: Array in object doesn't duplicate upon instantiating
                      Level 7
                      > That's odd. Is there a reason why this would only happen with the array
                      > class ?

                      It isn't only the array class. Its any object class

                      Reason is, that in Flash AS, the code on the RHS of the '=' for a var is
                      evaluated once when the class is defined.

                      So if you have

                      var s : String = "1234";

                      the "1234" is evaluated once (which is fine, its just a constant) and the
                      result saved and set as the default values for the var for each new
                      instance.

                      Similarly, if you have

                      var a : Array = new Array();

                      the 'new Array()' is evaluated once and the result saved and set as the
                      default values for the var for each new instance.

                      As you may already know, if a and b are arrays, and you set a = b; then a
                      will now refer to the identical array as b does (so changing a changes b and
                      vice versa).

                      That means, if you use the above way of defining an array variable (or
                      similarly for object), then each instance will have a reference to the one
                      actual array.

                      To fix it, just declate

                      var a : Array;

                      and in the constructor function have

                      a = new Array();

                      that wat the 'new Array()' happens for each new instance of the clase
                      (because the consutrctor function is run once for each new instance).
                      --
                      Jeckyl


                      • 8. Re: Array in object doesn't duplicate upon instantiating
                        Jean-P. Level 1
                        Jeckl,

                        Thanks for you clarification. With the background you provided it makes perfect sense. Its just a little counte-intuitive at first because you expect anything in a object to start fresh with each instance.

                        Thanks to all who helped.
                        • 9. Re: Array in object doesn't duplicate upon instantiating
                          Level 7
                          > Thanks for you clarification. With the background you provided it makes
                          > perfect sense. Its just a little counte-intuitive at first because you
                          > expect
                          > anything in a object to start fresh with each instance.

                          You're welcome.

                          NOTE: It really is a bug in the way Macromedia decided to implement classes.
                          They COULD have generated code that did the creation of members all at
                          instantiation time. According to the ECMA language standard, that is how it
                          SHOULD work. Macromedia just used the syntax but made it work differently
                          .. just like the way they use the same syntax for typed variables but
                          implement it 'wrong'.

                          AS3 (from what I understand) corrects these bugs and actually follows the
                          standard for not only cosmetically syntax, but (just as importantly) how
                          things work.
                          --
                          Jeckyl