| The Queue class is a type of collection in which items are added to the
end of the collection, and are removed from the start of the collection, much like a line
for the store checkout. This is often described as first in, first out (FIFO) behaviour.
Such a collection is useful if there is a bottleneck in an application (such as validating
credit cards or despatching orders) and the items in the collection need to be processed
in a first come, first served basis. Note that the .NET Framework's Queue class should
not be confused with Microsoft Message Queuing, which is a completely different
and unrelated technology!
Creating an instance of the Queue class
To create an instance of a queue class, the New keyword can be used. There are two main
constructors of note. The first constructor of the class is without any arguments, i.e.
The second constructor specifies an integer as the initial size for the queue.
The following example creates a queue which can be used to hold 24 objects:
Dim MyQueue As New Queue(24)
Since the queue class automatically grows beyond the specified capacity if the
initial capacity was insufficient, it is not usually necessary to specify the size of the
queue. However, doing so, especially if you know the queue will be especially large, or
fixed in length, can lead to a slight application performance increase.
Note that when you are intending to use the Queue class, it is sometimes necessary to
import the Systems.Collections namespace by using the Imports System.Collections
VB.NET statement.
Adding objects to the Queue
To add an object to a queue, the Enqueue method is invoked. There is only
constructor for this method, with a single argument: the object to be added to the Queue.
Any object can be added to an instance of the Queue class - from basic objects such as
integers and strings to other classes and even other Queue classes. It is also possible to
add null values using the VB.NET Nothing keyword or the System.DBNull.Value
class. Each Queue can contain many different types of objects. The code below shows a
variety of objects added to an instance of a Queue class:
Dim MyQueue As New Queue(4)
Dim MyOtherQueue As New Queue
Dim MyNumber As Integer = 24
Dim MyText As String = "ASP Documentation Tool"
MyQueue.Enqueue(MyNumber)
MyQueue.Enqueue(MyText)
MyQueue.Enqueue(MyOtherQueue)
MyQueue.Enqueue(System.DBNull.Value)
Again, if adding the object to the queue causes the size of the queue to exceed
the current size of the queue, then the queue is automatically resized to allow the object
to be added to the queue.
Examining the Queue and removing objects from the Queue
An object may be removed from the queue by invoking the Dequeue() method.
Dim DequeuedObject As Object
DequeuedObject = MyQueue.Dequeue()
There may be occasions when you would like to examine an object before it is
removed from a queue. This is possible by making use of the Peek() method. This method is
identical to Dequeue, but it does not actually remove the object from the queue:
Dim DequeuedObject As Object
DequeuedObject = MyQueue.Peek()
To determine the number of items in the queue, the Count property always
contains an integer value for the number of items in the queue.
Resizing the Queue
The queue can be resized by invoking the TrimToSize() method. This method is helpful
for applications in which it is useful to minimise memory usage, as resizing the queue to
its current capacity reduces the amount of memory allocated to the queue object.
Emptying the Queue
There is also a Clear() method, which as its name suggests, can be used to completely
empty the queue class.
Searching the Queue
To find out if an object is in a queue, the Contains() method can be used. The object
to be searched for is used as the method's argument. Since the method invokes the
Object.Equals method, it cannot be used if the objects are not exactly the same. This
means that in the case of String objects, the search is case sensitive. The following
example illustrates this:
Dim MyQueue As New Queue()
Dim MyText As String = "ASPDocTool"
MyQueue.Enqueue(MyText)
If MyQueue.Contains("aspdoctool") Then
Response.Write("found aspdoctool")
Else
Response.Write("didn't find aspdoctool")
End If
The string "aspdoctool" will not be found in this Queue class due to
the case sensitive search being used by the Contains() method.
Iterating through objects in the Queue
It is possible to loop through the items in the queue by using the queue's GetEnumerator
method. Although an iterator cannot be used to modify the items in the queue, it allows
more sophisticated searching of the queue than is possible using the Contains() method.
While iterating the queue, the Current property can be used to find out about
the current item being iterated. The code sample below shows how to loop through the items
in a queue using the queue's GetEnumerator method:
'Create an enumerator for a queueand loops through
'the (objects) in the queue. Shows the type of each object
'in the queue and its string representation
Dim QueueEnumerator As IEnumerator= MyQueue.GetEnumerator()
While QueueEnumerator.MoveNext
Dim ObjectType As System.Type
ObjectType = QueueEnumerator.Current.GetType
Response.Write("object name =" & ObjectType.Name)
Response.Write(" string representation =" & _
QueueEnumerator.Current.ToString & "")
End While
Other useful Queue methods
The Clone() method can be used to create a copy of a queue:
Dim MyOtherQueue As Queue()
MyOtherQueue = MyQueue.Clone()
There are also two methods for converting a Queue to an array. The ToArray()
method creates a new array from a Queue. The CopyTo() method copies the Queue to an
existing array. The CopyTo method takes two arguments: the array to which the Queue needs
to be copied, and the starting position of the Queue's contents within the existing array.
Summary
To summarise, the Queue collection is one of the many benefits that the .NET Framework
offers over Visual Basic 6.0 and VBScript. The Queue collection is well worth considering
if you are building any type of system that has to process items with a resource that has
limited availability.
|