PC Review


Reply
Thread Tools Rate Thread

Array.Copy and Cloning an array don't seem to work differently

 
 
illegal.prime@gmail.com
Guest
Posts: n/a
 
      29th Aug 2006
I would like to have an array of objects (whose class I define) and
then just invoke either:
MyClass [] clonedArray = (MyClass[]) myArray.Clone();
OR
Array.Copy(myArray, clonedArray, myArray.Length);

and have an array of cloned objects (i.e. the new array of objects
aren't the objects contained in my original array). But instead it
seems necessary for me to have to iterate over my entire array and
individually clone each element in the array. I thought at least
Array.Copy would do this for me. Does anyone know why it doesn't or if
I'm missing something that would make this idea work? Or do I actually
have to manually iterate over my array to achieve this?

Here is the test code so you can see what I mean:

class TestPair : ICloneable
{
public string a;
public string b;

public TestPair (string aParam, string bParam)
{
a = aParam;
b = bParam;
}

public object Clone()
{
return new TestPair(a, b);
}
}

public static void TestCloneOfClassWithPair()
{
TestPair testCloneOfPair1 = new TestPair("blah", "meh");

TestPair [] testPairs = new TestPair[1];
testPairs[0] = testCloneOfPair1;

// the following tries to use the Clone method of the array
//TestPair [] testCloneOfPairs = (TestPair [])testPairs.Clone();
// the following tries using Array.Clone
/*TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
Array.Copy(testPairs, testCloneOfPairs, testCloneOfPairs.Length);*/
// Only the following actually succeeds in giving me an array of
elements that don't reference the original array
TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
for (int i = 0;i < testPairs.Length;i++)
{
testCloneOfPairs[i] = (TestPair)testPairs[i].Clone();
}

if (testPairs[0] == testCloneOfPairs[0])
Trace.WriteLine("same object is referenced");

testCloneOfPairs[0].a = "new value";
Trace.WriteLine("testPairs - a: "+testPairs[0].a+" b:
"+testPairs[0].b);
}

Suggestions of why neither of the two other techniques succeed?

Thanks,
Novice

 
Reply With Quote
 
 
 
 
Bruce Wood
Guest
Posts: n/a
 
      29th Aug 2006

(E-Mail Removed) wrote:
> I would like to have an array of objects (whose class I define) and
> then just invoke either:
> MyClass [] clonedArray = (MyClass[]) myArray.Clone();
> OR
> Array.Copy(myArray, clonedArray, myArray.Length);
>
> and have an array of cloned objects (i.e. the new array of objects
> aren't the objects contained in my original array). But instead it
> seems necessary for me to have to iterate over my entire array and
> individually clone each element in the array. I thought at least
> Array.Copy would do this for me. Does anyone know why it doesn't or if
> I'm missing something that would make this idea work? Or do I actually
> have to manually iterate over my array to achieve this?
>
> Here is the test code so you can see what I mean:
>
> class TestPair : ICloneable
> {
> public string a;
> public string b;
>
> public TestPair (string aParam, string bParam)
> {
> a = aParam;
> b = bParam;
> }
>
> public object Clone()
> {
> return new TestPair(a, b);
> }
> }
>
> public static void TestCloneOfClassWithPair()
> {
> TestPair testCloneOfPair1 = new TestPair("blah", "meh");
>
> TestPair [] testPairs = new TestPair[1];
> testPairs[0] = testCloneOfPair1;
>
> // the following tries to use the Clone method of the array
> //TestPair [] testCloneOfPairs = (TestPair [])testPairs.Clone();
> // the following tries using Array.Clone
> /*TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
> Array.Copy(testPairs, testCloneOfPairs, testCloneOfPairs.Length);*/
> // Only the following actually succeeds in giving me an array of
> elements that don't reference the original array
> TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
> for (int i = 0;i < testPairs.Length;i++)
> {
> testCloneOfPairs[i] = (TestPair)testPairs[i].Clone();
> }
>
> if (testPairs[0] == testCloneOfPairs[0])
> Trace.WriteLine("same object is referenced");
>
> testCloneOfPairs[0].a = "new value";
> Trace.WriteLine("testPairs - a: "+testPairs[0].a+" b:
> "+testPairs[0].b);
> }
>
> Suggestions of why neither of the two other techniques succeed?


By definition, default copies / clones are all shallow. You want a deep
copy.

In order to achieve this you have to either:

a) Define a static method in your TestPair class that clones an array:

public static TestPair[] CloneArray(TestPair[])
{
...
}

and then TestPair[] clone = TestPair.CloneArray(original);

or b) Define a whole new class that is a collection of TestPairs and
write a Clone method for it:

public class TestPairCollection ...
{
public object Clone()
{
...
}
}

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variant Array Copy, Array row contains Object Neal Zimm Microsoft Excel Programming 3 13th Apr 2010 03:21 PM
Fast copy method of sub array (=array range) possible? Thomas Lebrecht Microsoft VB .NET 0 19th Mar 2009 08:49 AM
Array.Copy and Cloning an array don't seem to work differently illegal.prime@gmail.com Microsoft Dot NET Framework 1 29th Aug 2006 12:13 AM
select variables ranges, copy to array, paste the array in new workbook Mathew Microsoft Excel Worksheet Functions 1 1st Apr 2005 09:40 AM
Funcs work differently as array formula? Jonathan Rynd Microsoft Excel Worksheet Functions 3 22nd Jan 2004 03:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:32 PM.