Chinta
Chinta

Reputation: 275

ArrayCollection issue in flex

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?

Upvotes: 0

Views: 293

Answers (1)

MonkeyMagiic
MonkeyMagiic

Reputation: 138

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.

Upvotes: 1

Related Questions