O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Stack.CopyTo Method

Class

System.Collections.Stack

Syntax

                  stackvariable.CopyTo(array, index)
array (required; Array of Objects)

Array to which to copy the stack’s objects

index (required; Integer)

The index of the first array element to receive an element of the stack

Return Value

None

Description

Copies the stack elements into an array, starting at a specified array index

Rules at a Glance

  • The array can be of any data type that is compatible with the stack elements. Thus, for instance, we cannot use an Integer array to hold stack elements that are strings (that is, Objects whose subtype is String).

  • The array must be sized to accommodate the elements of the stack prior to calling the CopyTo method.

Example

Public Sub Main

' Define a new stack
Dim s As New Stack(  )
Dim aStack(  ), oItem As Object

' Push some items onto stack
s.Push("Chopin")
s.Push("Mozart")
s.Push("Beethoven")

' Size the array and copy to it
Redim aStack(s.Count - 1)
s.CopyTo(aStack, 0)

For Each oItem in aStack
   Console.WriteLine(oItem)
Next

End Sub

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required