Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everyone, its been a long time since I've posted, but here goes. I've been recently working on an application and I've hit abit of a snag. I seem to be hitting some sort of a problem which appears to be something todo with references.

 

If I create 2 blank arrays and then set them both equal to a third array, any changes I make to any of the arrays reflects in the others.

 

int[] a = new int[4];

int[] b = new int[4];

int[] c = new int[] { 1,2,3,4 };

 

a = c;

b = c;

 

b[0] = 2;

 

After running this section of code its obvious that a[0], b[0] and c[0] are now all equal to 2. This obviously means that by assigning the values equal I have created some kind of reference linking the objects, rather than copying the values. In an attempt to avoid this problem I tried using the CopyTo() command. So...

 

int[] a = new int[4];

int[] b = new int[4];

int[] c = new int[] { 1,2,3,4 };

 

c.CopyTo(a, 0);

c.CopyTo(b, 0);

 

b[0] = 2;

 

I thought this had finally solved my problem, as there was no longer a link between the arrays. The problem is I was only using the int arrays to simplify the method and detect what was going wrong. The actual section of code I wish to use employs arrays of custom classes. Here is an example.

 

Square[] a = new Square[iDimensions*iDimensions];

Square[] b = new Square[iDimensions*iDimensions];

 

arrGrid.CopyTo(a, 0);

arrGrid.CopyTo(b, 0);

 

b[2].SetValue("A");

 

Square is a simple class with a few properties and methods. Now as far as I can tell there is no difference in the logic from this code and the example using int arrays. However with this example the last line of code changes all 3 arrays.

 

Boy that took alot of explaining, I just hope it makes sense. Oh btw I'm using C#.

Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

Arrays themselves are reference types, hence the problems you were having in the first case as a, b and would all point to the same memory location.

The array.CopyTo will copy the elements from one array to another, however if the array is an array of classes (reference types) then the array just contains pointers to the objects in memory; copying the array will copy the pointers not what the pointers point to!

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
The array.CopyTo will copy the elements from one array to another, however if the array is an array of classes (reference types) then the array just contains pointers to the objects in memory;

 

Thanks for confirming that, from the result I was getting I was assuming this was the case but wasn't sure. So if both those methods simply create pointers when working with arrays of classes, what is the easiest way to create a seperate copy of an array. I basically need two arrays containing identical classes so that I can perform a set of operation on one array, but still have an array containing the initial states.

Anybody looking for a graduate programmer (Midlands, England)?
Posted
Probably the easiest way is to serialize your class to a memory stream and then deserialize it back into a new array variable.

 

I've been having a poke around the various methods offered, as it is an array it has a method 'Clone' I've not throughly checked this out yet, but would this not create a non-referenced copy of the array?

Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

Clone just creates a shallow copy of the array (a bit like assigning a reference to another variable).

IIRC there is an IClonable (or similar) interface you could make your Square class implement, search MSDN and you should get some examples on it.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...