RE: [flexcoders] Array reference vs values
I think this has been explained several times already in this thread, but you don't seem to have understood so I'll try explaining with some ASCII art. Before assigning timeArr[2] = null you have person.lunch > ^ | timeArr[2] --- After assigning timeArr[2] = null you have person.lunch > timeArr[2] --> You are not nulling the Date instance. (I don't even know what that would mean.) You are nulling the 2nd element in the timeArr Array which referred to the Date instance. Basically, AS reference are like C++ pointers, not like C++ references. If you are trying to represent a sequence of times, don't use five vars in Person named wakeup, breakfast, lunch, dinner, and bedtime. Use a single Array with five elements var times:Array /* of Date /; and use the constants static const WAKEUP:int = 0; static const BREAKFAST:int = 1; static const LUNCH:int = 2; static const DINNER:int = 3; static const BEDTIME:int = 4; to access them as times[LUNCH], etc. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Darren Houle Sent: Wednesday, July 16, 2008 6:56 PM To: flexcoders@yahoogroups.com Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren To: flexcoders@yahoogroups.com From: [EMAIL PROTECTED] Date: Thu, 17 Jul 2008 11:30:29 +1000 Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > wrote: This might be a basic AS question, but I encountered it building a Flex app, so... I create a Date var date:Date = new Date(); I create an Array var arr:Array = new Array(); I set the first element of the array equal to the date arr[0] = date; I create another date variable that refers to the first array element var ref:Date = arr[0] as Date; if I trace("date="+date+", arr[0]="+arr[0]+", ref="+ref) I get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 Now I null out the ref variable ref=null; Since AS uses references instead of values I would expect to null out the original date, but no... If I trace("date="+date+", arr[0]="+arr[0]+", ref="+ref) I now get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=null I thought everything was a reference in AS? Shouldn't nulling ref also null arr[0] which would null out date? Ultim
RE: [flexcoders] Array reference vs values
If you're trying to say that Array is passed by value, that's incorrect. Array is passed by reference, just like Object. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Sherif Abdou Sent: Wednesday, July 16, 2008 7:03 PM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Array reference vs values I think you may be able to do what you are doing by using the ByteArray class but I would wait for someone that knows more than me. The only things that get passed by reference is any class that inherits from an Object and Array is a native Flash player class in C++. - Original Message From: Sherif Abdou <[EMAIL PROTECTED]> To: flexcoders@yahoogroups.com Sent: Wednesday, July 16, 2008 8:59:54 PM Subject: Re: [flexcoders] Array reference vs values If i remember correctly since I am mixing too much C++,ActionScript, Coldfusion and learning them all the same time. Any of the primitive stuff in Actionscript get passed only by value and not by reference so you can't do what you are doing. - Original Message From: Darren Houle <[EMAIL PROTECTED] com> To: [EMAIL PROTECTED] ups.com Sent: Wednesday, July 16, 2008 8:55:33 PM Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren To: [EMAIL PROTECTED] ups.com From: [EMAIL PROTECTED] com Date: Thu, 17 Jul 2008 11:30:29 +1000 Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED] com <mailto:[EMAIL PROTECTED]> > wrote: This might be a basic AS question, but I encountered it building a Flex app, so... I create a Date var date:Date = new Date(); I create an Array var arr:Array = new Array(); I set the first element of the array equal to the date arr[0] = date; I create another date variable that refers to the first array element var ref:Date = arr[0] as Date; if I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 Now I null out the ref variable ref=null; Since AS uses references instead of values I would expect to null out the original date, but no... If I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I now get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=null I thought everything was a reference in AS? Shouldn't nulling ref also null arr[0] which would null out date? Ultimately.. . I have a custom object that has several fields each cont
RE: [flexcoders] Array reference vs values
Darren, Josh seems to have resolved your problem, but if you're interested, here's a brief discussion of how references are working here: >But since these references don't seem to propogate backwards, nulling >one of the array elements doesn't affect the original property. That's >the *whole purpose* of reference vs value... a reference is a pointer >to memory space... so if I null that memory space it should affect all >the vars pointing to that memory space. The way references work in ActionScript (and in Java, C#, and most others I'm familiar with), if you assign null to a reference, you are not "null[ing] that memory space". You are nulling that *reference*. It's your *reference* that now points to nothing--you're not actually changing the contents of that memory location. The behavior you're expecting could probably be achieved by another level of indirection in references, but I'm not aware of any language that actually does things this way. You could even achieve this in your own code, by providing a wrapper class for Dates that exposes the functions setDate(), getDate(), but Josh's suggestions is a better solution. -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: Darren Houle <[EMAIL PROTECTED]> Reply-To: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com Subject: RE: [flexcoders] Array reference vs values Date: Wed, 16 Jul 2008 21:55:33 -0400 Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren ____________ To: flexcoders@yahoogroups.com From: [EMAIL PROTECTED] Date: Thu, 17 Jul 2008 11:30:29 +1000 Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED]> wrote: This might be a basic AS question, but I encountered it building a Flex app, so... I create a Date var date:Date = new Date(); I create an Array var arr:Array = new Array(); I set the first element of the array equal to the date arr[0] = date; I create another date variable that refers to the first array element var ref:Date = arr[0] as Date; if I trace("date="+date+", arr[0]="+arr[0]+", ref="+ref) I get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 Now I null out the ref variable ref=null; Since AS uses references instead of values I would expec
RE: [flexcoders] Array reference vs values
Josh Okay, so here's what I did... var person : Person = new Person(); person.wakeup = new Date();person.breakfast = new Date();person.lunch = new Date();person.dinner = null;person.bedtime = null; var wfArr : Array = new Array(); timeArr[0] = "wakeup";timeArr[1] = "breakfast";timeArr[2] = "lunch";timeArr[3] = "dinner";timeArr[4] = "bedtime"; var event : int;if (some criteria) { event = 2; } if (some criteria) { person[timeArr[event]] = null; }and it works... person.lunch... aka person["lunch"]... was set to null... thanks, you rock!! Darren To: [EMAIL PROTECTED]: [EMAIL PROTECTED]: Thu, 17 Jul 2008 12:18:35 +1000Subject: Re: [flexcoders] Array reference vs values On Thu, Jul 17, 2008 at 11:55 AM, Darren Houle <[EMAIL PROTECTED]> wrote: Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date();person.breakfast = new Date();person.lunch = new Date();person.dinner = null;person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup;timeArr[1] = person.breakfast;timeArr[2] = person.lunch;timeArr[3] = person.dinner;timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int;if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense?DarrenI think so. But you're definitely going about it the wrong way - hear me out:In ECMAScript, you can access any public field by indexing its name as a string. Now assuming person has these fields, and you want to be able to access them (and mess with them) in order. There's a few ways to do this:const fieldOrder : Array = ['wakeup','breakfast','lunch','dinner', 'bedtime', ];Then you can do this:trace(myPerson.lunch); // == date.toString()myPerson[fieldOrder[2]] = null;trace(myPerson.lunch); // == nullBut while you can mute that instance of date, you can't delete it. It defeats the purpose of garbage collection (and probably makes it a lot harder to implement).If say, myPerson is *dynamic* either by the dynamic keyword on the class definition, or because it's created with "{}" instead of "new ClassName()", you can also do thisdelete myPerson[fieldOrder[2]];trace(myPerson.lunch); // === undefined, or an exception is thrown depending on various conditions of myPerson :)This will completely remove the "lunch" field from the myPerson instance, but the Date instance itself will still be sitting around waiting for garbage collection.-Josh-- "Therefore, send not to know For whom the bell tolls. It tolls for thee.":: Josh 'G-Funk' McDonald:: 0437 221 380 :: [EMAIL PROTECTED]
Re: [flexcoders] Array reference vs values
BWAHAHAHAHAHA! *wipes tear* That's awesome :) On Thu, Jul 17, 2008 at 12:53 PM, Sid Maskit <[EMAIL PROTECTED]> wrote: > Oops. Where my speech recognition software wrote "teen reference", it > should read "dereference". > -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]
Re: [flexcoders] Array reference vs values
Oops. Where my speech recognition software wrote "teen reference", it should read "dereference". - Original Message From: Sid Maskit <[EMAIL PROTECTED]> To: flexcoders@yahoogroups.com Sent: Wednesday, July 16, 2008 7:50:07 PM Subject: Re: [flexcoders] Array reference vs values It has been a while since I worked in C++, but I am pretty sure that you are incorrect about references. If I remember correctly, C++ has a distinction between pointers and references where a pointer points at a specific memory address, but a reference is an alias for another variable, and that variable might or might not itself be a pointer. The actual value of a pointer is a memory address, but the value of a reference is the value of whatever variable it refers to. In order to get the value of the object at the memory address, one has to teen reference the pointer. These differences occur because pointers and references used different mechanisms. Like most, maybe all, more recent languages, ActionScript has references, but not pointers. A reference is simply an alias which gives one access to some object. It is sort of like a telephone connection to the object: i.e. you can use it to set or get the value of the object; but hanging up the phone only breaks the connection, it does not make the object go away. You never have access to memory addresses. When an object has no references to it, it becomes eligible for garbage collection. There is no way to determine when an object will in fact be garbage collected. Applying this to the system you are describing, things look like this. (For simplicity, I will only deal with one of your dates.) var person:Person = new Person(); Assuming that you are not assigning default values in your class, at this point person.breakfast is null. person.breakfast = new Date(); Now person.breakfast refers to the newly created date object: i.e. the date object now has one reference to it. timeArr[1] = person.breakfast; Now timeArr[1] refers to the same date object as does person.breakfast: i.e. the date object now has two references to it. You could now access the date object by using either reference, and each of them would give you the same information, and the same ability to manipulate the date object. However, setting a reference to null, or to a different object, is essentially hanging up the telephone, and dialing a new number. This will connect the variable you are using to that new object, but has no effect on other variables referring to the previous object. timeArr[1] = null; Now timeArr[1] no longer refers to anything, but person.breakfast continues to refer to the original date object. Perhaps the key detail here is that what we have is to variables that refer to the same object, but those variables have no relationship with one another. This is one of the fundamental aspects of how object-oriented programming works. I would not say that one could not have a system where having two variables refer to the same object would put them into a relationship with one another, but only that that would be a very different system from the one we have. Having said that, I would say that you are going to need to have a way of directly accessing your person object if you want to set one or more of its properties to null. - Original Message From: Darren Houle <[EMAIL PROTECTED] com> To: [EMAIL PROTECTED] ups.com Sent: Wednesday, July 16, 2008 6:55:33 PM Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property
Re: [flexcoders] Array reference vs values
It has been a while since I worked in C++, but I am pretty sure that you are incorrect about references. If I remember correctly, C++ has a distinction between pointers and references where a pointer points at a specific memory address, but a reference is an alias for another variable, and that variable might or might not itself be a pointer. The actual value of a pointer is a memory address, but the value of a reference is the value of whatever variable it refers to. In order to get the value of the object at the memory address, one has to teen reference the pointer. These differences occur because pointers and references used different mechanisms. Like most, maybe all, more recent languages, ActionScript has references, but not pointers. A reference is simply an alias which gives one access to some object. It is sort of like a telephone connection to the object: i.e. you can use it to set or get the value of the object; but hanging up the phone only breaks the connection, it does not make the object go away. You never have access to memory addresses. When an object has no references to it, it becomes eligible for garbage collection. There is no way to determine when an object will in fact be garbage collected. Applying this to the system you are describing, things look like this. (For simplicity, I will only deal with one of your dates.) var person:Person = new Person(); Assuming that you are not assigning default values in your class, at this point person.breakfast is null. person.breakfast = new Date(); Now person.breakfast refers to the newly created date object: i.e. the date object now has one reference to it. timeArr[1] = person.breakfast; Now timeArr[1] refers to the same date object as does person.breakfast: i.e. the date object now has two references to it. You could now access the date object by using either reference, and each of them would give you the same information, and the same ability to manipulate the date object. However, setting a reference to null, or to a different object, is essentially hanging up the telephone, and dialing a new number. This will connect the variable you are using to that new object, but has no effect on other variables referring to the previous object. timeArr[1] = null; Now timeArr[1] no longer refers to anything, but person.breakfast continues to refer to the original date object. Perhaps the key detail here is that what we have is to variables that refer to the same object, but those variables have no relationship with one another. This is one of the fundamental aspects of how object-oriented programming works. I would not say that one could not have a system where having two variables refer to the same object would put them into a relationship with one another, but only that that would be a very different system from the one we have. Having said that, I would say that you are going to need to have a way of directly accessing your person object if you want to set one or more of its properties to null. - Original Message From: Darren Houle <[EMAIL PROTECTED]> To: flexcoders@yahoogroups.com Sent: Wednesday, July 16, 2008 6:55:33 PM Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren To: [EMAIL PROTECTED] ups.com From: [EMAIL PROTECTED
Re: [flexcoders] Array reference vs values
On Thu, Jul 17, 2008 at 11:55 AM, Darren Houle <[EMAIL PROTECTED]> wrote: > Josh > > Yes, what you're describing is exactly what I described and is, in fact, > what it happening... but to say I don't ever need to do this? Well... > yes... I need to do this... and it has nothing to do with the garbage > collector. > > Here, let me explain in another way > > I have a custom object... lets say it's a Person object. It has various > properties, but several are Date types. These are all consecutive, like a > workflow, and I want to be able to address them in order via an array... > like this... > > var person : Person = new Person(); > > person.wakeup = new Date(); > person.breakfast = new Date(); > person.lunch = new Date(); > person.dinner = null; > person.bedtime = null; > > var timeArr : Array = new Array(); > > timeArr[0] = person.wakeup; > timeArr[1] = person.breakfast; > timeArr[2] = person.lunch; > timeArr[3] = person.dinner; > timeArr[4] = person.bedtime; > > > Then some other code figures out where we are in the flow of the day's > events... > > var status : int; > if (some criteria) > { event = 2; } > > But I determine lunch hasn't actually happened yet, so it shouldn't have a > Date yet. I need to blank out this value that was previously set in the > Person object... > > if (some criteria) > { timeArr[event] = null; } > > But since these references don't seem to propogate backwards, nulling one > of the array elements doesn't affect the original property. That's the > *whole purpose* of reference vs value... a reference is a pointer to memory > space... so if I null that memory space it should affect all the vars > pointing to that memory space. > > Does that make more sense? > > Darren > > > I think so. But you're definitely going about it the wrong way - hear me out: In ECMAScript, you can access any public field by indexing its name as a string. Now assuming person has these fields, and you want to be able to access them (and mess with them) in order. There's a few ways to do this: const fieldOrder : Array = [ 'wakeup', 'breakfast', 'lunch', 'dinner', 'bedtime', ]; Then you can do this: trace(myPerson.lunch); // == date.toString() myPerson[fieldOrder[2]] = null; trace(myPerson.lunch); // == null But while you can mute that instance of date, you can't delete it. It defeats the purpose of garbage collection (and probably makes it a lot harder to implement). If say, myPerson is *dynamic* either by the dynamic keyword on the class definition, or because it's created with "{}" instead of "new ClassName()", you can also do this delete myPerson[fieldOrder[2]]; trace(myPerson.lunch); // === undefined, or an exception is thrown depending on various conditions of myPerson :) This will completely remove the "lunch" field from the myPerson instance, but the Date instance itself will still be sitting around waiting for garbage collection. -Josh -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]
Re: [flexcoders] Array reference vs values
I think you may be able to do what you are doing by using the ByteArray class but I would wait for someone that knows more than me. The only things that get passed by reference is any class that inherits from an Object and Array is a native Flash player class in C++. - Original Message From: Sherif Abdou <[EMAIL PROTECTED]> To: flexcoders@yahoogroups.com Sent: Wednesday, July 16, 2008 8:59:54 PM Subject: Re: [flexcoders] Array reference vs values If i remember correctly since I am mixing too much C++,ActionScript, Coldfusion and learning them all the same time. Any of the primitive stuff in Actionscript get passed only by value and not by reference so you can't do what you are doing. - Original Message From: Darren Houle <[EMAIL PROTECTED] com> To: [EMAIL PROTECTED] ups.com Sent: Wednesday, July 16, 2008 8:55:33 PM Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren To: [EMAIL PROTECTED] ups.com From: [EMAIL PROTECTED] com Date: Thu, 17 Jul 2008 11:30:29 +1000 Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED] com> wrote: This might be a basic AS question, but I encountered it building a Flex app, so... I create a Date var date:Date = new Date(); I create an Array var arr:Array = new Array(); I set the first element of the array equal to the date arr[0] = date; I create another date variable that refers to the first array element var ref:Date = arr[0] as Date; if I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 Now I null out the ref variable ref=null; Since AS uses references instead of values I would expect to null out the original date, but no... If I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I now get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=null I thought everything was a reference in AS? Shouldn't nulling ref also null arr[0] which would null out date? Ultimately.. . I have a custom object that has several fields each containing a Date. I create an array and make each element in the array = each Date in the custom obj. I need to be able to null an array element and have it carry backwards into the object and null out the original Date. Any ideas? Am I missing something really obvious? - - -- -- Flexcoders Mailing List FAQ: http://groups. yahoo.com/ group/flexcoders /files/flexcoder sFAQ.txt Search Archives:
Re: [flexcoders] Array reference vs values
If i remember correctly since I am mixing too much C++,ActionScript,Coldfusion and learning them all the same time. Any of the primitive stuff in Actionscript get passed only by value and not by reference so you can't do what you are doing. - Original Message From: Darren Houle <[EMAIL PROTECTED]> To: flexcoders@yahoogroups.com Sent: Wednesday, July 16, 2008 8:55:33 PM Subject: RE: [flexcoders] Array reference vs values Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date(); person.breakfast = new Date(); person.lunch = new Date(); person.dinner = null; person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup; timeArr[1] = person.breakfast; timeArr[2] = person.lunch; timeArr[3] = person.dinner; timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense? Darren To: [EMAIL PROTECTED] ups.com From: [EMAIL PROTECTED] com Date: Thu, 17 Jul 2008 11:30:29 +1000 Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED] com> wrote: This might be a basic AS question, but I encountered it building a Flex app, so... I create a Date var date:Date = new Date(); I create an Array var arr:Array = new Array(); I set the first element of the array equal to the date arr[0] = date; I create another date variable that refers to the first array element var ref:Date = arr[0] as Date; if I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 Now I null out the ref variable ref=null; Since AS uses references instead of values I would expect to null out the original date, but no... If I trace("date= "+date+", arr[0]="+arr[ 0]+", ref="+ref) I now get date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 GMT-0400 2008, ref=null I thought everything was a reference in AS? Shouldn't nulling ref also null arr[0] which would null out date? Ultimately.. . I have a custom object that has several fields each containing a Date. I create an array and make each element in the array = each Date in the custom obj. I need to be able to null an array element and have it carry backwards into the object and null out the original Date. Any ideas? Am I missing something really obvious? - - -- -- Flexcoders Mailing List FAQ: http://groups. yahoo.com/ group/flexcoders /files/flexcoder sFAQ.txt Search Archives: http://www.mail- archive.com/ flexcoders% 40yahoogroups. comYahoo! Groups Links -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED] com
RE: [flexcoders] Array reference vs values
Josh Yes, what you're describing is exactly what I described and is, in fact, what it happening... but to say I don't ever need to do this? Well... yes... I need to do this... and it has nothing to do with the garbage collector. Here, let me explain in another way I have a custom object... lets say it's a Person object. It has various properties, but several are Date types. These are all consecutive, like a workflow, and I want to be able to address them in order via an array... like this... var person : Person = new Person(); person.wakeup = new Date();person.breakfast = new Date();person.lunch = new Date();person.dinner = null;person.bedtime = null; var timeArr : Array = new Array(); timeArr[0] = person.wakeup;timeArr[1] = person.breakfast;timeArr[2] = person.lunch;timeArr[3] = person.dinner;timeArr[4] = person.bedtime; Then some other code figures out where we are in the flow of the day's events... var status : int; if (some criteria) { event = 2; } But I determine lunch hasn't actually happened yet, so it shouldn't have a Date yet. I need to blank out this value that was previously set in the Person object... if (some criteria) { timeArr[event] = null; } But since these references don't seem to propogate backwards, nulling one of the array elements doesn't affect the original property. That's the *whole purpose* of reference vs value... a reference is a pointer to memory space... so if I null that memory space it should affect all the vars pointing to that memory space. Does that make more sense?Darren To: [EMAIL PROTECTED]: [EMAIL PROTECTED]: Thu, 17 Jul 2008 11:30:29 +1000Subject: Re: [flexcoders] Array reference vs values When you do this: var date:Date = new Date();You're creating an instance of Date and a reference to it named "date".When you do this: var ref:Date = arr[0] as Date;You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector.-Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED]> wrote:
Re: [flexcoders] Array reference vs values
When you do this: var date:Date = new Date(); You're creating an instance of Date and a reference to it named "date". When you do this: var ref:Date = arr[0] as Date; You're creating another reference to the same instance, this time your reference is named "ref". So when you set ref = null, you're making "ref" point to nothing. "date" and "arr[0]" remain unchanged. You don't need to, nor can you remove the date instance created above. That's the job of the garbage collector. -Josh On Thu, Jul 17, 2008 at 11:25 AM, Darren Houle <[EMAIL PROTECTED]> wrote: > This might be a basic AS question, but I encountered it building a > Flex app, so... > > I create a Date > >var date:Date = new Date(); > > I create an Array > >var arr:Array = new Array(); > > I set the first element of the array equal to the date > >arr[0] = date; > > I create another date variable that refers to the first array element > >var ref:Date = arr[0] as Date; > > if I trace("date="+date+", arr[0]="+arr[0]+", ref="+ref) I get > > date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 > GMT-0400 2008, ref=Wed Jul 16 21:04:45 GMT-0400 2008 > > Now I null out the ref variable > >ref=null; > > Since AS uses references instead of values I would expect to null out > the original date, but no... > > If I trace("date="+date+", arr[0]="+arr[0]+", ref="+ref) I now get > > date=Wed Jul 16 21:04:45 GMT-0400 2008, arr[0]=Wed Jul 16 21:04:45 > GMT-0400 2008, ref=null > > I thought everything was a reference in AS? Shouldn't nulling ref > also null arr[0] which would null out date? > > Ultimately... I have a custom object that has several fields each > containing a Date. I create an array and make each element in the > array = each Date in the custom obj. I need to be able to null an > array element and have it carry backwards into the object and null > out the original Date. Any ideas? Am I missing something really > obvious? > > > > > -- > Flexcoders Mailing List > FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt > Search Archives: > http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups > Links > > > > -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]