PDA

View Full Version : Flex remove link between to variables



Lone wolf
June 10th, 2008, 11:54 AM
Greetings!

I've banging my head against the wall for a few days now about this problem:

Code:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

public var myArr:ArrayCollection = new ArrayCollection(new Array("1","2","3","4","5"));

public var myTest:ArrayCollection = new ArrayCollection()

public function init():void{
myTest = new ArrayCollection(myArr.source);
myTest.removeItemAt(2);
trace(myTest.length);
trace(myArr.length);
}
]]>
</mx:Script>
</mx:Application>



the code above results in an output of

Code:


4
4



But I want it to result in
Code:


4
5

Iamthejuggler
June 10th, 2008, 12:15 PM
I believe that because you are pointing it to the arrayCollection's source it isn't making a new instance of that source, it is using the same source for both arrays, so when you change one both arraycollections change. Change the line to:


myTest = new ArrayCollection(myArr.toArray());

and that should work.

Lone wolf
June 10th, 2008, 01:25 PM
i'll give it a go!

but why doesn't this occure with Strings for example??

thanks!!