• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

How do I copy an array into another?

Hello,

I want to do something like this: ArrayCopy = Array

Without the original Array changing when I change the CopyArray.

Please help.

Thanks in advance.

0
richardhaeger
Asked:
richardhaeger
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
Jorge PaulinoIT Pro/DeveloperCommented:
0
 
gangwischCommented:
Dim ArrayOfInts() As Integer = {1,2,3,4,5,6,7,8,9}
Dim My2ndArrayOfInts() as Integer = ArrayOfInts.Clone()
0
 
Jaime OlivaresSoftware ArchitectCommented:
use the Clone() method:

arr2 = arr1.Clone()

considering arr1 and arr2 are defined as arrays
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Jaime OlivaresSoftware ArchitectCommented:
Oops, too late.
0
 
Stephen MandersonSoftware EngineerCommented:
Use the array.Clone() method

Regards
Steve
0
 
Stephen MandersonSoftware EngineerCommented:
Crap me too :D
0
 
richardhaegerAuthor Commented:
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
 
Jorge PaulinoIT Pro/DeveloperCommented:
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)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Brachiosaurus")
        dinosaurs.Add("Compsognathus")

        ' 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now