3 Replies Latest reply on Jul 5, 2006 12:42 AM by Chris_007

    How to manage Arrays as Object members

    Chris_007
      I found a realy strange behavior with the instanciation of a Class which contains an Array member.

      Here is the code wich is self explanatory.



        • 1. Re: How to manage Arrays as Object members
          Level 7
          Create the array instance in the constructor.

          class MyObj {

          public var tab:Array;

          function MyObj(index) {
          this.tab= new Array();
          }

          }

          "Chris_007" <webforumsuser@macromedia.com> wrote in message
          news:e8e7do$7gf$1@forums.macromedia.com...
          > I found a realy strange behavior with the instanciation of a Class which
          > contains an Array member.
          >
          > Here is the code wich is self explanatory.
          >
          >
          >
          >
          >
          > class MyObj {
          > //
          > public var tab:Array = new Array();
          > private var objectName:String;
          > private var theIndex:Number;
          > //
          > // Constructeur
          > function MyObj(index) {
          > this.theIndex = index;
          > // Object name
          > this.objectName = "name#" + index;
          > for (var i = 0; i < 3; i++) {
          > this.tab = index * 3 + i;
          > }
          > }
          > //
          > function printObject() {
          > trace(" Printing Object name --> " + this.objectName);
          > for (var i = 0; i < 3; i++) {
          > trace(" tab[" + i + "] : " + this.tab
          );
          > }
          > }
          > //
          > function getClone() {
          > return new MyObj(theIndex);
          > }
          > }
          >
          >
          > // And the main scenario code
          > //
          > var index:Number = 0;
          > //
          > var objArray:Array = new Array();
          > //
          > var oob:MyObj;
          > //
          > function printMyArray(arr:Array):Void {
          > for (var i = 0; i < arr.length; i++) {
          > trace(" tab[" + i + "] : " + arr );
          > }
          > }
          > //
          > // Filling in the Array
          > for (var i = 0; i < 4; i++) {
          > oob = new MyObj(index);
          > // oobj = oob.getClone();
          > trace("-- New object #" + i + " created. MyObj[" + index + "]");
          > oob.printObject();
          > objArray
          = oob;
          > trace("-- Now entered into the array -- printing from object #" + i );
          > objArray .printObject();
          > trace("-- or printing from the scenario -- objArray[" + i + "] is realy
          > stored");
          > printMyArray(objArray
          .tab);
          > trace("____________________________________");
          > index += 1;
          > }
          > //
          > // Printing the array of pointers to MyObj
          > trace("___________________________________________");
          > trace("And now, all in one...\nPrinting the array of pointers to MyObj
          !!!!");
          > trace("___________________________________________");
          > for (var i = 0; i < 4; i++) {
          > objArray .printObject();
          > }
          > trace("------ Or directly from the scenario -------");
          > for (var i = 0; i < 4; i++) {
          > trace(" Printing from scenario Object name --> " +
          objArray
          .objectName);
          > printMyArray(objArray .tab);
          > }
          > trace("___________________________________________");
          > trace("They are all the same ?!\nIt seems to have lost something !!!!");
          > trace("___________________________________________");
          > //
          > //
          > trace("\nAmazingly, the array's elements do not refer to the same
          object\n");
          > //
          > trace("objArray[0] == objArray[0] ? -->" + (objArray[0] == objArray[0]));
          > trace("objArray[0] == objArray[1] ? -->" + (objArray[0] == objArray[1]));
          > trace("objArray[0] instanceof MyObj ? -->" + (objArray[0] instanceof
          MyObj));
          > trace("");
          > //
          > trace("\nThey realy seem to point different objects !\n");
          > //
          > // So, why do they have different outputs ?
          > //
          > //
          > // *******************************************************************
          > // It seems the 'new' constructor creates a new Object 'MyObj'
          > // which always resides at the same memory location.
          > // 'oob' is in fact a pointer, and objArray[] an array of pointers,
          > // as so an array of memory addresses.
          > // The objArray[] elements are pointers which always refer to the
          > // the same memory zone, which in fact contains the last content of the
          > object.
          > // *******************************************************************
          > //
          > // And then manualy
          > //
          > trace("**********************************************************");
          > trace("\nAnd then another way to create each new object manualy\n");
          > trace("**********************************************************");
          > //
          > //
          > var otherObjArray:Array = new Array();
          > //
          > var oob0 = new MyObj(100);
          > trace("-- New object created #100");
          > oob0.printObject();
          > otherObjArray[0] = oob0;
          > trace("-- Now entered into the array -- otherObjArray[0]");
          > otherObjArray[0].printObject();
          > //
          > var oob1 = new MyObj(200);
          > trace("-- New object created #200");
          > oob1.printObject();
          > otherObjArray[1] = oob1;
          > trace("-- Now entered into the array -- otherObjArray[1]");
          > otherObjArray[1].printObject();
          > //
          > var oob2 = new MyObj(300);
          > trace("-- New object created #300");
          > oob2.printObject();
          > otherObjArray[2] = oob2;
          > trace("-- Now entered into the array -- otherObjArray[2]");
          > otherObjArray[2].printObject();
          > //
          > trace("___________________________________________");
          > trace("And now, all in one...\nEnjoy the show !!!!");
          > trace("___________________________________________");
          > otherObjArray[0].printObject();
          > otherObjArray[1].printObject();
          > otherObjArray[2].printObject();
          > //
          > trace("\nThe problem seems to reside in the way the Arrays members are
          managed
          > by the Objects !\n");
          > trace("\nHELP me please to find the bug\n");
          > //
          >


          • 2. Re: How to manage Arrays as Object members
            Level 7
            This is 'correct' behaviour for Flash

            You should only DECALRE your array var in the class definition .. and assign
            it a new array value in the constructor.

            I could explain why that behaviour happens if you like .. but don't have
            time right now for a lengthy reply
            --
            Jeckyl


            • 3. Re: How to manage Arrays as Object members
              Chris_007 Level 1
              Thanks to you all for your quick answer.

              I just woke up this morning, and realised I found myself the solution during the night.
              It's always a real joy to solve a problem on wich we stayed some hours without solution.

              A particular message to Jeckyl :
              I am very interested in the reason why "This is 'correct' behaviour for Flash", because I thought the creation of the tab:Array in the class declaration of properties was enough to be repeated on each instanciation of this class.

              Why not ?