Queue Class


Queue Class

Namespace

System.Collections (standard version)

System.Collections.Generic (generic version)

Creatable

Yes

Description

A Queue object implements a "first in, first out" (FIFO) data structure. Items are added in a line (queue), with new items placed at the end of the line. Only the item at the beginning of the line can be removed. Its real-world counterpart is a line for the ticket counter at a movie theater.

The queue includes features for adding items (Enqueue), removing items (Dequeue), and counting the items in the queue (Count), among other features. Objects of any type may be added to the queue.

The following table lists some of the more useful and interesting members of the Queue class. Those marked with an asterisk (*) have separate entries in this chapter.

Member

Description

Clear Method

Removes all items from the queue

Clone Method

Makes a distinct copy of the queue and its members

Contains Method *

Indicates whether a specific object is in the queue

CopyTo Method *

Copies queue elements to an existing array

Count Property

Indicates the number of items currently in the queue

Dequeue Method *

Removes and returns the beginning item in the queue

Enqueue Method *

Adds a new item to the end of the queue

IsReadOnly Property

Indicates whether the queue is read-only or not

Peek Method *

Returns the beginning queue item without removing it

ToArray Method *

Copies the queue to a new array


Example

This sample code shows the basic use of the queue.

     ' ----- Add some basic items to a queue.     Dim nameQueue As New Queue     nameQueue.Enqueue("Chopin")     nameQueue.Enqueue("Mozart")     nameQueue.Enqueue("Beethoven")     ' ----- Examine and return the items.     MsgBox(nameQueue.Peek(  ))     ' Displays "Chopin"     MsgBox(nameQueue.Dequeue(  ))  ' Displays "Chopin"     MsgBox(nameQueue.Dequeue(  ))  ' Displays "Mozart"     ' ----- Remove the remaining items.     MsgBox(nameQueue.Count)    ' Displays 1 (for Beethoven)     nameQueue.Clear(  ) 

Version Differences

Visual Basic 2005 adds support for generics to several collection-style classes, including the Queue class. The version of the Queue class that supports generics appears in the System.Collections.Generic namespace. Generics are discussed in Chapter 10.

See Also

Collection Class, Hashtable Class, Stack Class




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