Hi,
I've read through all of the posts here on pointers and also this
page - http://www.mischel.com/diary/2006/07/24.htm, but can someone
clear up something that's been bothering me please?
Can a function change the value of a non-primitive type that's passed
as a parameter?
If I pass a reference to an object as a function parameter, the local
function variable then holds the address of the object on the heap.
Fine.
If I then attempt to make changes to this object within the function,
the address of the local function reference variable changes to
another address on the heap and creates a new object. The original
object is not updated. We end up with two reference variables
pointing to two different objects on the heap.
I thought the point of "pass by reference" for non-primitives in AS3
was to be able to manipulate objects directly by passing the hex
address of the object on the heap (as in c++)
I understand that a solution is to return the local reference to the
temporary object from the function and assign it to the class
instance variable, so this is more an exercise in understanding the
essential workings of AS3.
Here's some code.
----- AS3 Code starts -----
private var collectionAC:ArrayCollection = new ArrayCollection();
// Debug: value of collectionAC is @4deb859
private function resultHandler(event:ResultEvent, ac:ArrayCollection)
{
// Debug: value of ac is also @4deb859
// There are now 2 reference variables pointing to
// one object on the heap
ac = event.result.products.rug;
// Debug: value of ac is now @4f839e1
// ?WHY? does this line create a new object scoped to
// the function and not modify the original object directly?
}
------ MXML code starts ----------
<mx:HTTPService id="collectionService"
url="data/collection.xml"
result="resultHandler(event, collectionAC);"/>
------ Code ends --------
Many thanks, Rich