Stack: ToArray

ToArray

Copies the Stack to a new array.



 Public Function ToArray ( ) As Variant ( )

Return Values

Variant() -  A new array containing copies of the elements of the Stack.

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.

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

CopyTo