No reference!!!

A

Arjen

Hi,

I have an arraylist.
I copy the objects in this arraylist to a second arraylist.
When I edit the objects in the second arraylist then the objects in the
first arraylist are changed too.
I don't want this!!!

I tried to work with arraylist.clone() but this seems not make any
difference.

I am adding my objects now with arraylist2.add(object from arraylist one).
After that I read the arraylist2 object and change it. And then arraylist
one is changed too... how can I stop this?

Are there options?

Thanks,
Arjen
 
J

Joanna Carter [TeamB]

"Arjen" <[email protected]> a écrit dans le message de [email protected]...

| I have an arraylist.
| I copy the objects in this arraylist to a second arraylist.
| When I edit the objects in the second arraylist then the objects in the
| first arraylist are changed too.
| I don't want this!!!
|
| I tried to work with arraylist.clone() but this seems not make any
| difference.
|
| I am adding my objects now with arraylist2.add(object from arraylist one).
| After that I read the arraylist2 object and change it. And then arraylist
| one is changed too... how can I stop this?

If you hold more than one reference to any reference type, you will always
be pointing at the same object. If you want to copy the contents of objects,
then you need either a copy constructor and/or to implement ICloneable.

class MyClass : ICloneable
{
private data string;

...

public MyClass(value string) // default constructor
{
data = value;
}

public MyClass(other MyClass)
{
data = other.data;
...
}

object ICloneable.Clone()
{
return new MyClass(this);
}
}

Joanna
 
C

chanmm

Hi Arjen,

Yeap, Joanna has done it in a elegant way. But for the idiot prove solution
I will just:

1. Create an obj to take back the obj from Arraylist
2. Insert the obj again to the second arraylist

for(int i=0; i < arrA.count;i++)
{
obj objA = new obj();
objA = arrA(i);
arrB.Add(objA);
}

chanmm
 
J

Joanna Carter [TeamB]

"chanmm" <[email protected]> a écrit dans le message de %[email protected]...

| 1. Create an obj to take back the obj from Arraylist
| 2. Insert the obj again to the second arraylist
|
| for(int i=0; i < arrA.count;i++)
| {
| obj objA = new obj();
| objA = arrA(i);
| arrB.Add(objA);
| }

Sorry, but this will end up just the same as the original problem.

| for(int i=0; i < arrA.count;i++)
| {
| obj objA = new obj();

This creates a new object.

| objA = arrA(i);

But this simply overwrites the new object with a reference to an existing
object in the first list, and throws away the newly created object.

| arrB.Add(objA);

Therefore, this simply adds a reference to the original object in the
original list. So altering the object in the second list will also alter the
object in the first list, as they are still the same object.

Joanna
 
K

Kevin Spencer

Incorrrect, I'm afraid. The problem is not copying the array; it is
assigning the object references in it to the second array. The objects in
this particular array must be reference types, which means that
(essentially) the variables are managed pointers to the objects themselves.
When you assign from one array to the other, you are assigning the
references; therefore, the pointers are still pointing to the same
instances.

To get a copy that is not a reference, each object's value type members must
be copied to a new instance of the same type of object.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.
 
J

Joanna Carter [TeamB]

"Kevin Spencer" <[email protected]> a écrit dans le message de [email protected]...

| Incorrrect, I'm afraid. The problem is not copying the array; it is
| assigning the object references in it to the second array. The objects in
| this particular array must be reference types, which means that
| (essentially) the variables are managed pointers to the objects
themselves.
| When you assign from one array to the other, you are assigning the
| references; therefore, the pointers are still pointing to the same
| instances.
|
| To get a copy that is not a reference, each object's value type members
must
| be copied to a new instance of the same type of object.

Which is why I demonstrated a method of copying object contents using a copy
constructor and/or ICloneable.

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top