His description indicated that he expected a var like ac to behave like a C reference. But that's not how AS works; a var like ac behaves like a C pointer. - Gordon
________________________________ From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Jerome Clarke Sent: Friday, November 09, 2007 9:34 PM To: [email protected] Subject: Re: [flexcoders] Re: Reference variables / pointers / hex heap addresses I think what he's asking is why is it when he points to ac = new ArrayCollection(); say to for example address 1... but then later tells ActionScript ac = event.result.products.rug; and the address changes to 2 I think he was expecting ac to still point to address 1... but with the value of address 2 ( copied basically ) I might be wrong but as far as I know... unlike C/C++... AS3 isn't fully native like C/C++ so... allocating/deallocating memory is going to be slower... it's faster for the Flash player to allocate new memory, change the address to the new memory, dump the old address in garbage ( for to be garbage collected later ) rather than reallocating the memory... also AS3 doesn't exactly work like C/C++ anyways just like some other languages like PHP On Nov 9, 2007 10:02 PM, Gordon Smith <[EMAIL PROTECTED] <mailto:gosmith%40adobe.com> > wrote: > > > > > > > > Object references in ActionScript are like C pointers, not like C > references. The statement > > > ac = event.result.products.rug; > > changes the pointer ac so that instead of pointing to the ArrayCollection > that you new'd, it now points to the rug. > > By the way, this makes no sense since ac is typed as an ArrayCollection, > while products.rug is presumably NOT an ArrayCollection. The compiler can't > report this error because it doesn't know what type event.result is. If the > result object has some type, you should cast it: > > ac = SomeResultClass(event.result).products.rug > > > > why can I not directly manipulate my original object from within the > function? > > You can access and change the state of that object within the function.. For > example, within the function you can do things like > > ac.addItem() > trace(ac.length) > > to manipulate your ArrayCollection. But if you want your function to make > collectionAC point to a different object, you must write > > collectionAC = ... > > inside that function. > > Does that help? > > - Gordon > > ________________________________ > From: [email protected] <mailto:flexcoders%40yahoogroups.com> [mailto:[email protected] <mailto:flexcoders%40yahoogroups.com> ] On > Behalf Of quiet.mountain > Sent: Friday, November 09, 2007 10:19 AM > To: [email protected] <mailto:flexcoders%40yahoogroups.com> > Subject: [flexcoders] Re: Reference variables / pointers / hex heap > addresses > > > > > > > > > Hi, > > Thanks for the reply, and yes I am coming from a c background. > > Even though "the address itself is passed by value" (passing a pointer by > copy) the result is still a locally scoped function variable that points to > the original object (see debug comments below). > > "so you cannot change it [the address]" I don't think I want to change it > (the value of the class reference variable that points to the original > object). I want to make changes to the original object itself (which I now > have an address for via the pointer passed by copy). > > My question is why does the value of this locally scoped function variable > automatically change to point to a newly created object of the same type > when I attempt to make changes to it (see debug comments below). > > Or to put it another way. If my locally scoped function variable is now > actually pointing to my original object why can I not directly manipulate my > original object from within the function? > > Thanks, Rich > > > > --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "Ralf Bokelberg" <[EMAIL PROTECTED]> > wrote: > > > > Hi Rich > > > > Are you coming from a c background? > > Logically a object parameter is passed by reference, which means, the > > address is passed to the method instead of a copy of the object's > > values. > > Technically the address itself is passed by value though, so you > > cannot change it. > > > > Cheers > > Ralf. > > > > On Nov 9, 2007 1:03 PM, quiet.mountain <[EMAIL PROTECTED]> wrote: > > > > > > > > > > > > > > > > > > > > > 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, <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.prod! ucts.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 > > > > > > > > > > > > > > -- > > Ralf Bokelberg <[EMAIL PROTECTED]> > > Flex & Flash Consultant based in Cologne/Germany > > Phone +49 (0) 221 530 15 35 > > > > > >

