0
votes

I have an array collection into which im adding different model values as below.

var ob:Object=new Object();
ob.name=string1;
ob.data=model.arraylist1;
ob.id=model.arraylist2;
nextArrayCollection.addItem(ob);

//model.arraylist1 value is changed here
//model.arraylist2 value is changed here

ob=new Object();
ob.name=string1;
ob.data=model.arraylist1;
ob.id=model.arraylist2;
nextArrayCollection.addItem(ob);

The issue is that when the second item is added to the nextArrayCollection the value of the first item in arraycollection also changes to same as the second item added.

I am really confused at what is happenning here. Each time i add new item to the nextArrayCollection all the existing items value changes to that of the new one added. Is the arraycollection using the refrence and not the value. How can i overcome this issue?

1
its not due to ArrayCollection, but because of same refrence, for item 2 use "ob2"Imran
i tried that too but did nt workChinta
it didn't work because both @Imran comment and MonkeyMagiic answer pointed out 1 of both of your problems. 1) you need different objects ie var ob:Object = new Object() and var ob2:Object = new Object(). setting ob=blah in the second add just updates the original (and its references). 2) you are pointing the values (data, name, id) to the SAME model properties... thus all objects (even if you used ob1, ob2, etc) are all pointing to the same data. So if the model changes, ALL references to the model get updated.Jason Reeves

1 Answers

1
votes

I believe this is just a misunderstanding of OOP and it's use of referencing objects:

Even though you add two new objects (ob = new object()) you point both at your model properties, this is NOT copied when assigned but ONLY referenced.

An easy test would be to simply clone the collection:

ob=new Object();
ob.name=string1;
ob.data= objectUtil.clone(model.arraylist1);
ob.id=model.arraylist2;
nextArrayCollection.addItem(ob);

this is not an ideal paradigm/model for your data structure though, I would think the resolution would be to sort the data out and not set the data to the model.arraylist1 and model.arraylist2.