Stack.CopyTo Method


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




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net