
452
|
Chapter 12: The Language Reference
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
See Also
Stack Class
Stack.CopyTo Method
Class
System.Collections.Stack (standard version)
System.Collections.Generic.Stack (generic version)
Syntax
stackVariable.CopyTo(array, index)
array (required; compatible array)
Array to which to copy the stack’s objects
index (required; Integer)
The index of the first zero-based array element to receive a stack member
Description
The CopyTo method copies the stack elements into an existing array, starting at a
specified array index.
Usage at a Glance
• The array can be of any data type that is compatible with the stack elements. An
array of
Integer can accept Short stack elements but not String elements.
• The array must be sized to accommodate the elements of the stack prior to calling
the CopyTo method.
Example
The sample code copies stack items to an array.
Dim nameStack As New Stack
Dim nameArray( ) As Object
' ----- Build the stack.
nameStack.Push("Chopin")
nameStack.Push("Mozart")
nameStack.Push("Beethoven")
' ----- Size the array and copy elements.
ReDim nameArray(nameStack.Count - 1)
nameStack.CopyTo(nameArray, 0)
Version Differences
Visual Basic 2005 adds support for generics to stacks, as discussed in Chapter 10.
See Also
Stack Class, Stack.ToArray Method