Solved

# How do I copy an array into another?

Posted on 2008-06-20
764 Views
Hello,

I want to do something like this: ArrayCopy = Array

Without the original Array changing when I change the CopyArray.

0
Question by:richardhaeger
• 2
• 2
• 2
• +2

LVL 48

Assisted Solution

jpaulino earned 200 total points
ID: 21833899
0

LVL 10

Accepted Solution

gangwisch earned 300 total points
ID: 21833979
Dim ArrayOfInts() As Integer = {1,2,3,4,5,6,7,8,9}
Dim My2ndArrayOfInts() as Integer = ArrayOfInts.Clone()
0

LVL 55

Expert Comment

ID: 21833995
use the Clone() method:

arr2 = arr1.Clone()

considering arr1 and arr2 are defined as arrays
0

LVL 55

Expert Comment

ID: 21834003
Oops, too late.
0

LVL 19

Expert Comment

ID: 21834008
Use the array.Clone() method

Regards
Steve
0

LVL 19

Expert Comment

ID: 21834010
Crap me too :D
0

Author Closing Comment

ID: 31469283
Both solutions worked fine, I gave Paulino less points because he did not include an example. The clone method does a shallow copy. Does CopyTo do a deep copy? I guess so, am I correct?
0

LVL 48

Expert Comment

ID: 21835189
The example was in the MSDN link. I just didn't found usefull to copy/paste. :)

The CopyTo allows to copy to a new array and gives you the possibility to select the start index. Ex from MSDN adapted:

Dim dinosaurs As New List(Of String)

' Shows the current array List(Of String)
Debug.WriteLine(vbLf & "Contents of the dinosaurs :")
For Each dinosaur As String In dinosaurs
Debug.WriteLine(dinosaur)
Next

' Declare an array with 15 elements (0 through 14).
Dim array(14) As String

' Adds some text to the first position
array(0) = "OldItem"

' Copy the array dinosaurs to the array() starting on 1
dinosaurs.CopyTo(array, 1)

Debug.WriteLine(vbLf & "Contents of the array:")
For Each dinosaur As String In array
Debug.WriteLine(dinosaur)
Next

And the results:

Contents of the dinosaurs :
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Contents of the array:
OldItem
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recentâ€¦
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points withâ€¦
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundrâ€¦