3 Replies Latest reply on Apr 12, 2011 2:57 PM by ssardar

    Weird issue with Array

    ssardar Level 1

      Has anyone ever seen anything like this??

       

      I have written 2 nested loops at the end of which an object is added to an array.

       

      But for some reason, when all is done, all the objects I have in the array are exactly the same, there are mutiple, but they all have the same values and reference number...I'm really confused as to why this would happen I must've done this over a thousand times and never seen this.  I've had the other guy who does flex look at it as well...and he couldn't see why either, in fact he said he has "done it over a thousand times..."  Well you get the picture.

      Here is my loop segment if anyone can see why this happens please tell me.  Thanks.

       

      This is Flex version 3:

       

      ------------------------------------------------------------------

       

      //loop through grid

       

       

      var i:int = 0

       

      for(i=0; i < gridData.length;i++){

       

      //loop though deliverables and grab date values where they exisit for each project or row of gridData

       

       

      var gTemp:Object = Object(gridData.getItemAt(i)); //gridDataVO

       

       

      var j:int = 0;

       

      for(j=0; j <tempQueue.length; j++){//loop through tempQueue, holds deliverables

       

       

      var qTemp:Object = Object(tempQueue.getItemAt(j));

       

       

      //inspect all deliverable items for each project row and create a new Object and push to array.

       

       

      var s:String = this.parentApplication.mGrid.elGetAppName(qTemp.eventID);//this function just fetches the appfield value

       

       

      if(gTemp[s] == null || gTemp[s].length ==0){

      qTemp =

      null;//do nothing

      }

       

      else{

      qTemp.projectID = gTemp.pdNO;

      qTemp.dueDate = gTemp[s];

       

      workQueue.addItem(qTemp);

      qTemp=

      null;

      }

       

       

      //

      }

       

      //for

      gTemp=

       

      null;

      }

      //for outer

       

       

       

      ----------------------------------------------------------------------

       

       

      basically the workQueue  array collection ends up with all the same objects when this is done.

        • 1. Re: Weird issue with Array
          Flex harUI Adobe Employee

          Was everything in tempqueue already the same object?

          • 2. Re: Weird issue with Array
            ssardar Level 1

            There are 2 objects in tempQueue(inner loop) and they represent the deliverables for the user.  They are not the same object.

             

            But given object 1 has a value of deliverable 1, and object 2 has a value of deliverable two (which in this instance isn't pushed into the workQueue array as it does not meet the criteria)

             

            But I do grab object 1 and add otehr values into the object and push it into the workQueue.

            Then the outer loop iteration brings it back and I grab object 1 again for the next project.

             

            If that's what you mean then yes it is the same object having different values set to it, based on the projects in the outer loop.

             

            Could this be causing it?  I thought de-referencing the qTemp by setting it to null would solve this.  I know this works in Java, but it seems there is an inherent difference I am unaware of.

             

            So how would I solve this?

            • 3. Re: Weird issue with Array
              ssardar Level 1

              Never mind, as always you helped me answer my own question.

               

              I just looked up that actionscript is pass by reference as default (I guess something I should've looked up before I ever started coding )

               

               

              but fixed it using this function

               

              public function clone(object:Object):* {
              var byteArray:ByteArray = new ByteArray();
              byteArray.writeObject(object);
              byteArray.position = 0;
              return byteArray.readObject();
              }

               

              (for anyone else)

               

               

              Thanks again.