Name

Queue.CopyTo Method

Class

System.Collections.Queue

Syntax

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

Array to which to copy the queue’s objects

index (required; Integer)

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

Return Value

None

Description

Copies the queue 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 queue elements. Thus, for instance, we cannot use an Integer array to hold queue elements that are strings (that is, Objects whose subtype is String).

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

Example

' Define a new queue
Dim q As New Queue(  )
Dim aQueue(  ), oItem As Object

' Queue up some items
q.Enqueue("Chopin")
q.Enqueue("Mozart")
q.Enqueue("Beethoven")

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

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

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.