CorArray: Copy

Copy

Copies one Array to another Array and performs type casting as necessary.



 Public Sub Copy(
	  ByRef SourceArray As Variant,
	  ByRef DestinationArray As Variant,
	  ByVal Length As Long )

Parameters

SourceArray
[ByRef] Variant. The array from which to copy the elements.
DestinationArray
[ByRef] Variant. The array in which to place the elements.
Length
[ByVal] Long. The number of elements to copy.

Remarks

The SourceArray and DestinationArray parameters must have the same number of dimensions. In addition, DestinationArray must already have been dimensioned and must have a sufficient number of elements to accommodate the copied data.

When copying between multidimensional arrays, the array behaves like a long one-dimensional array, where the rows (or columns) are conceptually laid end to end. For example, if an array has three rows (or columns) with four elements each, copying six elements from the beginning of the array would copy all four elements of the first row (or column) and the first two elements of the second row (or column).

Exceptions

ExceptionCondition
ArgumentNullException SourceArray is null.
-or-
DestinationArray is null.
RankExceptionSourceArray and DestinationArray have different ranks.
ArrayTypeMismatchExceptionSourceArray and DestinationArray are incompatible types.
InvalidCastExceptionAt least one element in SourceArray cannot be cast to the type of DestinationArray.
ArgumentOutOfRangeExceptionLength is less than zero.
ArgumentException Length is greater than the number of elements from SourceIndex to the end of SourceArray.
-or-
Length is greater than the number of elements from DestinationIndex to the end of DestinationArray.

Examples

The following code example uses CorArray.Copy and CorArray.CopyEx to copy elements between an Integer array and a Variant array.

Public Sub Main()
    Dim MyIntArray() As Integer
    Dim MyVarArray() As Variant
    
    ' Creates and initializes a new Integer array and a new Variant array.
    MyIntArray = NewIntegers(1, 2, 3, 4, 5)
    MyVarArray = NewVariants(26, 27, 28, 29, 30)
    
    ' Prints the initial values of both arrays.
    Debug.Print "Initially,"
    Debug.Print "Integer Array: ";
    PrintValues MyIntArray
    Debug.Print "Variant Array: ";
    PrintValues MyVarArray
    
    ' Copies the first two elements from the Integer array to the Variant array.
    CorArray.Copy MyIntArray, MyVarArray, 2
    
    ' Prints the values of the modified arrays.
    Debug.Print t("\nAfter copying the first two elements of the Integer array to the Variant array,")
    Debug.Print "Integer Array: ";
    PrintValues MyIntArray
    Debug.Print "Variant Array: ";
    PrintValues MyVarArray
    
    ' Copies the last two elements from the Object array to the integer array.
    CorArray.CopyEx MyVarArray, UBound(MyVarArray) - 1, MyIntArray, UBound(MyIntArray) - 1, 2
    
    ' Prints the values of the modified arrays.
    Debug.Print t("\nAfter copying the last two elements of the Object array to the integer array,")
    Debug.Print "Integer Array: ";
    PrintValues MyIntArray
    Debug.Print "Variant Array: ";
    PrintValues MyVarArray
End Sub

Private Sub PrintValues(ByRef MyArr As Variant)
    Dim Value As Variant
    
    For Each Value In MyArr
        Debug.Print CorString.Format("{0,5}", Value);
    Next
    
    Debug.Print
End Sub

' The code example produces the following output.
'    Initially,
'    Integer Array:     1    2    3    4    5
'    Variant Array:    26   27   28   29   30
'
'    After copying the first two elements of the Integer array to the Variant array,
'    Integer Array:     1    2    3    4    5
'    Variant Array:     1    2   28   29   30
'
'    After copying the last two elements of the Object array to the integer array,
'    Integer Array:     1    2    3   29   30
'    Variant Array:     1    2   28   29   30

See Also

Project CorLib Overview

Class CorArray Overview

CopyEx