Solved

How do I copy an array into another?

Posted on 2008-06-20
8
765 Views
Last Modified: 2012-06-22
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
Comment
Question by:richardhaeger
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 200 total points
ID: 21833899
0
 
LVL 10

Accepted Solution

by:
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

by:Jaime Olivares
ID: 21833995
use the Clone() method:

arr2 = arr1.Clone()

considering arr1 and arr2 are defined as arrays
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21834003
Oops, too late.
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21834008
Use the array.Clone() method

Regards
Steve
0
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 21834010
Crap me too :D
0
 

Author Closing Comment

by:richardhaeger
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

by:jpaulino
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)

        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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 52
export data table to excel in vb.net 10 36
SqlServer no dupes 25 37
How do sunrise and sunset times change with altitude 14 35
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question