ArrayList: CopyToEx

CopyToEx

Copies a range of elements to an Array.



 Public Sub CopyToEx(
	  ByVal Index As Long,
	  ByRef Arr As Variant,
	  ByVal ArrayIndex As Long,
	  ByVal Count As Long )

Parameters

Index
[ByVal] Long. The zero-based index in the list to start copying from.
Arr
[ByRef] Variant. The array to copy the items to.
ArrayIndex
[ByVal] Long. The index in the destination array to start copying items to.
Count
[ByVal] Long. The number of items to copy.

Remarks

The destination array must be large enough to hold the number of elements being copied.

Exceptions

ExceptionCondition
ArgumentNullExceptionThe Arr is uninitialized.
ArgumentOutOfRangeException Index is less than zero.
-or-
ArrayIndex is less than zero.
-or-
Count is less than zero.
ArgumentException Arr is multidimension.
-or-
Index is equal to or greater than Count of the source ArrayList.
-or-
The number of elements from Index to the end of the source ArrayList is greater than the available space from ArrayIndex to the end of the destination Arr.
InvalidCastException Elements in the ArrayList cannot be converted to a compatible type of the destination Arr.

Examples

This example shows how to copy elements from an ArrayList into an existing array, replacing any elements in the destination array.

Private Sub Main()
    Dim Words As New ArrayList
    Dim Story() As String

    ' Create our initial story in a regular String array.
    Story = NewStrings("Humpty", "Dumpty", "sat", "on", "a", "wall.")
    
    ' Display the current story.
    Debug.Print "The initial story is:"
    PrintValues Story
    
    ' Create a new story in an ArrayList.
    Words.Add "Peter"
    Words.Add "Rabbit"
    Words.Add "slept"
    Words.Add "under"
    Words.Add "a"
    Words.Add "tree."
    
    ' Copies the third word to same word location in the array.
    Words.CopyToEx 2, Story, 2, 1
    
    ' Display the new story.
    Debug.Print "The story with 'sat' substituted by 'slept' is:"
    PrintValues Story
    
    ' Copies the first two elements to the first
    ' two elements in the array.
    Words.CopyToEx 0, Story, 0, 2
    
    ' Display the story with the new name.
    Debug.Print "The story with the name changed is:"
    PrintValues Story
    
    ' Increase the size of the story array.
    ReDim Preserve Story(0 To 11)
    
    ' Append the words to the end of the current story.
    Words.CopyTo Story, 6
    
    ' Display the final story.
    Debug.Print "The final story is:"
    PrintValues Story
End Sub

Private Sub PrintValues(ByRef s() As String)
    Dim i As Long
    
    Debug.Print vbTab;
    
    For i = LBound(s) To UBound(s)
        Debug.Print CorString.Format("{0} ", s(i));
    Next i
    
    Debug.Print
End Sub

' This code produces the following output.
'
' The initial story is:
'     Humpty Dumpty sat on a wall.
' The story with 'sat' substituted by 'slept' is:
'     Humpty Dumpty slept on a wall.
' The story with the name changed is:
'     Peter Rabbit slept on a wall.
' The final story is:
'     Peter Rabbit slept on a wall. Peter Rabbit slept under a tree.

See Also

Project CorLib Overview

Class ArrayList Overview