The assignment

    _global.arrayFruit = fruitModel.fruit;

doesn't make a copy of the array; it just makes _global.arrayFruit contain 
another reference to the same array. This is how arrays and objects work in 
many languages, because making copies of large data structures is expensive and 
often multiple objects need to reference the same data.

If you need to make a copy, you can use

    _global.arrayFruit = fruitModel.fruit.slice(0);

The slice(start, end) method on an Array can be used to create a copy of any 
contiguous section of an Array. Here is a description of the two arguments:

start - A number specifying the index of the starting point for the slice. If 
start is a negative number, the starting point begins at the end of the array, 
where -1 is the last element.

end - A number specifying the index of the ending point for the slice. If you 
omit this parameter, the slice includes all elements from the start to the end 
of the array. If end is a negative number, the ending point is specified from 
the end of the array, where -1 is the last element.

By the way, we recommend never using _global in Flex applications; it is likely 
to go away in future versions.

- Gordon


-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of P 
Trisnadi
Sent: Tuesday, August 23, 2005 3:57 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Why changing global variable also changes the model data?

I have a global variable that is populated by a model's data:
     _global.arrayFruit = new Array();
     ...
     <mx:Model id="fruitModel" source="fruits.xml"/>
     ...
     _global.arrayFruit = fruitModel.fruit;
 
 Why does fruitModel.fruit also change when I do:
     _global.arrayFruit.removeItemAt(itemNum);
 
 I'm not changing fruitModel and want to keep it the same, so that I can 
re-populate global.arrayFruit after fruitModel. Any idea?
 
 Thanks in advanced,
 - ptrisnadi
 






--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links



 




------------------------ Yahoo! Groups Sponsor --------------------~--> 
<font face=arial size=-1><a 
href="http://us.ard.yahoo.com/SIG=12h196iev/M=362329.6886308.7839368.1510227/D=groups/S=1705007207:TM/Y=YAHOO/EXP=1124859957/A=2894321/R=0/SIG=11dvsfulr/*http://youthnoise.com/page.php?page_id=1992
">Fair play? Video games influencing politics. Click and talk back!</a>.</font>
--------------------------------------------------------------------~-> 

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to