Queue: Dequeue

Dequeue

Removes and returns the item at the beginning of the Queue.



 Public Function Dequeue ( ) As Variant

Return Values

Variant -  The object that is removed from the beginning of the Queue.

Remarks

This method is similar to the Peek method, but Peek does not modify the Queue.

Exceptions

ExceptionCondition
InvalidOperationException The Queue is empty.

Examples

The following example shows how to add elements to the Queue, remove elements from the Queue, or view the element at the beginning of the Queue.

Public Sub Main()
    Dim MyQ As New Queue
    
    MyQ.Enqueue "The"
    MyQ.Enqueue "quick"
    MyQ.Enqueue "brown"
    MyQ.Enqueue "fox"
    
    ' Display the Queue
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Removes an element from the queue.
    Debug.Print "(Dequeue)    " & MyQ.Dequeue
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Removes another element from the queue.
    Debug.Print "(Dequeue)    " & MyQ.Dequeue
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
    
    ' Views the first element in the queue but does not remove it.
    Debug.Print "(Peek)       " & MyQ.Peek
    
    ' Displays the queue.
    Debug.Print "Queue values:";
    PrintValues MyQ
End Sub

Private Sub PrintValues(ByVal MyCollection As IEnumerable)
    Dim Item As Variant
    
    For Each Item In MyCollection
        Debug.Print "    " & Item;
    Next
    
    Debug.Print
End Sub

' This example code produces the following output.
'
'    Queue values:    The    quick    brown    fox
'    (Dequeue)    The
'    Queue values:    quick    brown    fox
'    (Dequeue)    quick
'    Queue Values:    brown fox
'    (Peek)       brown
'    Queue Values:    brown fox

See Also

Project CorLib Overview

Class Queue Overview

Enqueue

Peek