Queue: CopyTo

CopyTo

Copies the Queue elements to an existing one-dimensional array, starting at the specified array index.



 Public Sub CopyTo(
	  ByRef Arr As Variant,
	  ByVal Index As Long )

Parameters

Arr
[ByRef] Variant. The one-dimensional array that is the destination of the elements copied from Queue.
Index
[ByVal] Long. The lowerbound-based index in Arr at which copying begins.

Remarks

The elements are copied to the array in the same order in which the enumerator iterates through the Queue.

This method is an O(n) operation, where n is Count.

Exceptions

ExceptionCondition
ArgumentNullException Arr is uninitialized.
ArgumentOutOfRangeException Index is less than lower-bound.
ArgumentException Arr is multidimensional.
-or-
The number of elements in the source Queue is greater than the available space from Index to the end of the destination Arr.
ArrayTypeMismatchException The type of the source Queue cannot be cast automatically to the type of the destination Arr.

Examples

The following example shows how to copy a Queue into a one-dimensional array.

Public Sub Main()
    Dim MySourceQ As New Queue
    Dim MyTargetArray() As String
    Dim MyStandardArray() As Variant
    
    ' Initializes the source Queue.
    MySourceQ.Enqueue "three"
    MySourceQ.Enqueue "napping"
    MySourceQ.Enqueue "cats"
    MySourceQ.Enqueue "in"
    MySourceQ.Enqueue "the"
    MySourceQ.Enqueue "barn"

    MyTargetArray = NewStrings("The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog")
    ReDim Preserve MyTargetArray(0 To 14)
    
    ' Displays the values of the target array.
    Debug.Print "The target Array contains the following (before and after copying):"
    PrintValues MyTargetArray
    
    ' Copies the entire source queue to the target array, starting at index 6.
    MySourceQ.CopyTo MyTargetArray, 6
    
    ' Displays the values of the target array.
    PrintValues MyTargetArray
    
    ' Copies the entire source queue to a new standard array.
    MyStandardArray = MySourceQ.ToArray
    
    ' Displays the values of the new standard array.
    Debug.Print "The new standard array contains the following:"
    PrintValues MyStandardArray
End Sub

Private Sub PrintValues(ByRef MyArr As Variant)
    Dim Item As Variant
    
    For Each Item In MyArr
        Debug.Print " " & Item;
    Next
    
    Debug.Print
End Sub

' This example code produces the following output.
'
'    The target Array contains the following (before and after copying):
'     The quick brown fox jumped over the lazy dog
'     The quick brown fox jumped over three napping cats in the barn
'    The new standard array contains the following:
'     three napping cats in the barn

See Also

Project CorLib Overview

Class Queue Overview

ToArray