O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

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

  • Using the first syntax, you can copy a range of values from the beginning of sourceArray to the beginning of destinationArray. Using the second syntax, you can copy a range of values from anywhere in destinationArray to anywhere in targetArray.

  • sourceArray and destinationArray must have the same number of dimensions.

  • length is the total number of elements to be copied. If sArr1 is a two- dimensional array, for example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required