VB.NET Language in a Nutshell

   
Array.Copy Method

Class

System.Array

Syntax

Array.Copy( sourceArray , destinationArray , length ) Array.Copy( sourceArray , sourceIndex , destinationArray , _ destinationIndex , length )

sourceArray (required; any array)

The array to be copied

sourceIndex (required in second overloaded version; integer)

The index in sourceArray at which copying begins

destinationArray (required; any array)

The target array

destinationIndex (required in second overloaded version; Integer)

The index in destinationArray where the first element is to be copied

length (required; Integer)

The number of elements to copy

Return Value

None

Description

Makes a copy of all or part of an array.

Since arrays are reference types, when we set one array variable equal to another, we are just assigning a new reference to the same array. For instance, consider the following code:

Dim a( ) As Integer = {1, 2, 3} Dim b( ) As Integer ' Array assignment b = a ' Change b b(0) = 10 ' Check a MsgBox(a(0)) 'Displays 10

The fact that changing b(0) also changes a(0) shows that a and b point to the same array.

Rules at a Glance

Example

Dim a( ) As Integer = {1, 2, 3} Dim c( ) As Integer ' Array copy ReDim c(UBound(a) + 1) Array.Copy(a, c, UBound(a) + 1) 'Change c c(0) = 20 'Check a MsgBox(a(0)) 'Displays 1

VB.NET/VB 6 Differences

Since arrays were not a reference type in VB 6, you could simply create a copy of an existing array through assignment, thus eliminating the need for a Copy method.

   

Категории