I wrote the code below :
1. MyClass[] origArr=new MyClass[3];
2. MyClass[] arr1;
3. // filled the array with objects and did some work on it .
4. dgv_1.DataSource=origArr;
5.
6. // Here is the problem :
7. arr1=origArr;
8. // do some work on arr1 ...
9. dgv_2.DataSource=arr1;
For some reason the data in ‘origArr’ changed when data in ‘arr1’ change …
I thought that maybe this happened because ‘origArr’ and ‘arr1’ are pointers referring to the same object so I changed line ‘7’ to :
7. origArr.CopyTo(arr1,0);
but it didn’t work … what can i do to make the pointers referring to different objects?
Your line 7 copies the reference to
origArrintoarr1, so they point to the same physical array.Even a
CloneorCopyTowon’t help much: you will get a copy of the array structure, but in there the references to your oringinal classes are copied. So you end up with a new array with still the same objects.You will need to Clone your MyClass objects and put those clones into a new array.