|
-
Dec 17th, 2005, 01:25 PM
#1
Thread Starter
PowerPoster
object copy problem
Am I being daft?
using .NET 2.0 here and VS.NET 2005
I know this works because I have done it millions of times over many years! But for some reason, its just not working.
lets say we have 2 objects:
1) a proper object that exists
2) a new object which is "null"
if we want the new object to be the same as the proper object that exists, would we usually not do this?
Code:
SomeClass theNewObject = this.theOldOriginalObject;
then if we wish to modify the new object properties, we can do so WITHOUT the old original objects' properties being modified?
Well, for some reason, when i do this, if i modify the new object's properties, the old objects' properties get modified also!!!!
how comes?!
Code:
SomeClass theNewObject = this.theOldOriginalObject;
theNewObject.SomeProperty = someValue;
//^that should NOT effect the oldoriginalobject property but it does
what am I doing wrong? Surely it cannot be because this class is an IComparable?
if i make very simple int variables and do the same thing as above (of course, with the same type of variables) then it works fine:
Code:
int a = 0;
int b = a;
b = 2;
//(a should remain uneffected and it does remain uneffected)
Last edited by Techno; Dec 17th, 2005 at 01:39 PM.
-
Dec 18th, 2005, 07:36 AM
#2
Fanatic Member
Re: object copy problem
Value Types and Reference Types
An Object is a reference type wich means that the reference gets copied rather than the actual data. So the new variable refers to the same object.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
-
Dec 18th, 2005, 08:10 AM
#3
Thread Starter
PowerPoster
Re: object copy problem
thanks for that. I actually have another problem
when the user modified an object's property - a NEW object instance is created. The values/properties for this NEW object is retrieved from the form.
when it is going to be added into the arraylist, it firstly REMOVES the OLD object, then it adds the NEW object.
Great, it works
BUT if we do this the second time round and beyond, when it tries to REMOVE the existing object, it doesnt do it! But the object exists in the arraylist! I just do not understand why its doing this.
Last edited by Techno; Dec 18th, 2005 at 08:14 AM.
-
Dec 18th, 2005, 09:14 AM
#4
Re: object copy problem
There is no such thing as an "object which is null". An object is a an object. A variable can be a null reference, i.e. it does not refer to any object. It seems to me that you don't really understand properly the concept of references and reference- and value-type objects. A variable is a section of memory in an area called the stack. When dealing with value-type objects, which includes all structures such as numerical types, Dates, Booleans, etc. then the object itself is stored on the stack in the variable. This means that while two variables can contain equivalent values, every variable contains a distinct object. When dealing with reference type objects, the object itself is stored on another area of memory called the heap. The variable on the stack actually contains the heap address of the object. Many variables can contain the same memory address so they would each refer to the same object. Change the object through any one of those references and it will be reflected through all of them, as there is only one object. Each item in an ArrayList is a rference-type variable. You could add the same object to an ArrayList 20 times and you find that the ArrayList would have 20 items, but each of them would refer to the same object. Change that object through any of those references and it will be reflected through every item. If you assign a different object to a variable at any point, you are just putting a different memory address in the variable. The original object is unaffected but the variable does not refer to it any more.
-
Dec 18th, 2005, 10:05 AM
#5
Thread Starter
PowerPoster
Re: object copy problem
I think you mis-understood the problem
-
Dec 18th, 2005, 06:42 PM
#6
Re: object copy problem
If you tell an ArrayList to Remove an item then it will be removed. Either there is a bug with the ArrayList that i'm not aware of or you are using incorrect code. The only way to know that is for you to post the code you are using.
-
Dec 18th, 2005, 06:47 PM
#7
Thread Starter
PowerPoster
Re: object copy problem
The problem was because I had to override equals and gethashcode, since that object class implemented IComparable.
It was also because of the referencing of objects and so on, and it seems to be working ok for now but still testing.
-
Dec 18th, 2005, 07:41 PM
#8
Re: object copy problem
Didn't look at the name or post count before making original post. Shouldn't post when half asleep. Grumpy-old-b*starder than usual. Apologies.
-
Dec 18th, 2005, 08:03 PM
#9
Thread Starter
PowerPoster
Re: object copy problem
lol - no worries
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|