Stack: CopyTo

CopyTo

Copies the Stack 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 Stack.
Index
[ByVal] Long. The lowerbound-based index in array at which copying begins.

Remarks

The elements are copied onto the array in last-in-first-out (LIFO) order, similar to the order of the elements returned by a succession of calls to Pop.

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 Stack is greater than the available space from Index to the end of the destination Arr.
ArrayTypeMismatchException The type of the source Stack cannot be cast automatically to the type of the destination Arr.

Examples

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

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

    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 stack 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 stack 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 Stack Overview

ToArray