Using stackoverflow.com to get quick answers to programming problems

Couple of days ago I was doing some Flex programming, and one thing bothered me while I was assigning one ArrrayCollection to another one. For example, let’s say we have 2 ArrayCollections caled firstAC and secondAC. firstAC has for instance 10 rows. Now I wanted secondAC to get all the data from firstAC, so I did secondAC = firstAC.

Then whatever I did with secondAC (use addItem or filterFunction) everything would show up in firstAC. Since I didnt quite know what was going on I used stackoverflow.com and posed this question:

Title: Changes to one variable propagates to another

For example I have two ArrayCollection’s – firstAC and secondAC. If I do secondAC = firstAC, and than I make changes to secondAC (prehaps put a filterfunction on it) it somehow propagates to firstAC, would anyone tell me why that happens in Flex or Actionscript 3?

What can I do if I only want secondAC to get all data from firstAC but then when I make changes to secondAC it does not show in firstAC?

Thanxs a bunch for answers! Ladislav

All I had to do is wait a while and answers came in:

Answer nr. 1: phtrivier

When you write secondAC = firstAC, you simply state that secondAC and firstAC are references to the same array collection.

What you want is to clone the first collection (as in, copy all elements one by one).

You should be able to do it with something like :

secondAC = new ArrayCollection();
secondAC.addAll(firstAC);
Answer nr. 2: Christopher W. Allen-Pool

In ECMAScript languages (AS1-3, JavaScript, et al.), when you use

var foo = //some value which is not a String or a Number

what you are really saying is “foo now points to the same object as that other variable.” This means that in this situation, both arrays will be the same value:

var foo:Array = [ 1, 2, 3 ];
foo = bar;
bar.push( 4 );
trace( foo ); // 1, 2, 3, 4

This also works for functions:

var foo:Array = [ 1, 2, 3 ];
adder( foo );
function adder( bar:Array ):void {
    bar.push( 4 );
}

trace( foo ); // 1, 2, 3, 4

and it even works with XML:

var xml:XML = <root><foo/></root>;
var bar:XML = xml;
bar.children()[ 0 ].@bar = 1;
trace( xml.toXMLString() ); // <root><foo bar="1"/></root>

This is called “passing by reference” instead of “passing by value” or “passing by copy”. It means that every time that an item is referenced, each variable will point to the same object.

There are many ways to get around this, and most of them depend on your context. For arrays, my favorite is Array.concat(), which returns a literal clone of the array. This means that anything I do to the returned value will not effect the original in any way. If I’m dealing with XML, however, I will do something like: var xml2:XML = XML( xml.toXMLString() );.

In your case, I would actually recommend that you use:

var secondAC:ArrayCollection = new ArrayCollection( firstAC.source.concat() );

This has the major benefits of not only being faster (it relies on compiled code instead of Flex SDK code and it also does not first instantiate a new array and then re-populate it), but it also has the distinct benefit of being available in older versions of Flex 3’s SDK — it is entirely backwards compatible.

As you can see you can get some really good answers quickly by posting a question on stackoverflow.com. It is nice to see how many people are willing to put in their time and effort to answer a fellow programmer’s question.

Thanx to all for replying on my threads on stackoverflow 🙂

Link to the original post: http://stackoverflow.com/questions/1868431/changes-to-one-variable-propagates-to-another/

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: