 |
BOOK: Professional VB.NET, 2nd Edition or 2003  | This is the forum to discuss the Wrox book Professional VB.NET, 2nd Edition by Fred Barwell, Richard Case, Bill Forgey, Billy Hollis, Tim McCarthy, Jonathan Pinnock, Richard Blair, Jonathan Crossland, Whitney Hankison, Rockford Lhotka, Jan D. Narkiewicz, Rama Ramachandran, Matthew Reynolds, John Roth, Bill Sheldon, Bill Sempf; ISBN: 9780764544002 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional VB.NET, 2nd Edition or 2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 8th, 2004, 11:00 PM
|
|
Registered User
|
|
Join Date: Nov 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Arrays
Hi I a beginner to DotNet. Can any one Please explain what is the difference between the Clone method and the Copy method for Arrays in VB.NET. Thanks.
|
|

November 9th, 2004, 05:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
|
|
The SharedCopy method does exactly what you think it will do, copy an array or pieces of an array to another array. Remember though, you may me copying reference types so approach it accordingly
Array.Copy(MyIntArray, MySecondIntArray, 2)
The Clone method is similar to copy, it makes a clone of the original array. However, just like copy, you have to be careful when you use clone becuase it makes a Shallow Copy (Shallow Clone) of the objects if they are reference types. As such, if you manipulate the second array it will change the values of the first array and vice versa! Remember also that this has NOTHING to do with Passing ByVal or ByRef. You can pass the cloned array ByVal, change its values, and both Array1 and Array2 will have their values changed!
MySecondIntArray = MyIntArray.Clone
The CopyTo method is another one I really love. This can copy the elements of an array to any object implementing the IList interface. On catch here though, it's only supported in the 1.1 version of the framework.
MyArrayList = Array.CopyTo(MyIntArray, 0)
Gokulan Ethiraj
|
|

February 21st, 2008, 02:28 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I felt the need of this posting because I have seen postings (not in this forum but in many others) saying that "CopyTo() makes a deep copy and Clone() makes a shallow copy." This is absolutely wrong.
Both CopyTo() and Clone() make shallow copy. Clone() method makes a clone of the original array. It returns an exact length array.
On the other hand, CopyTo() copies the elements from the original array to the destination array starting at the specified destination array index. Note that, this adds elements to an already existing array.
The following code will contradict the postings saying that CopyTo() makes a deep copy:
public class Test
{
public string s;
}
// Write Main() method and within it call test()
private void test()
{
Test[] array = new Test[1];
array[0] = new Test();
array[0].s = "ORIGINAL";
Test[] copy = new Test[1];
array.CopyTo(copy, 0);
// Next line displays "ORIGINAL"
MessageBox.Show("array[0].s = " + array[0].s);
copy[0].s = "CHANGED";
// Next line displays "CHANGED", showing that
// changing the copy also changes the original.
MessageBox.Show("array[0].s = " + array[0].s);
}
Let me explain it a bit. If the elements of the array are of reference types, then the copy (both for Clone() and CopyTo()) will be made upto the first(top) level. But the lower level doesn't get copied. If we need copy of lower level also, we have to do it explicitly. That's why after Cloning or Copying of reference type elements, each element in the Cloned or Copied array refers to the same memory location as referred by the corresponding element in the original array. This clearly indicates that no separate instance is created for lower level. And if it were so then changing the value of any element in the Copied or Cloned array would not have effect in the corresponding element of the original array.
I think that my explanation is exhaustive but I found no other way to make it understandable. Hope this will help everyone.
Asit Pal
|
|
The Following User Says Thank You to dotnetasit For This Useful Post:
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Help With Arrays |
Crippy |
Ruby |
2 |
March 6th, 2013 05:59 PM |
| arrays |
ozPATT |
Excel VBA |
2 |
November 4th, 2005 06:11 AM |
| Multidemmesional Arrays OR arrays |
gmoney060 |
Classic ASP Basics |
3 |
November 1st, 2004 03:42 PM |
| Arrays |
tajin |
Excel VBA |
0 |
June 20th, 2004 09:07 AM |
|
 |