Home
VB.NET
Queue Examples
This page was last reviewed on May 2, 2023.
Dot Net Perls
Queue. A Queue is a generic collection in VB.NET. It implements a FIFO algorithm. The element that is added first (with Enqueue) is also the first one that is removed (with Dequeue).
With Queue, you can keep a sliding cache of elements, with the one added first always being the first to be removed. It could be used to implement a request system.
Stack
First, this program shows the Enqueue function, which you use to add an element to the Queue. Next, it loops over the elements in the Queue with the For-Each looping construct.
Note For Each is implemented in a special way so that it shows all the internal elements in the Queue.
For
Module Module1 Sub Main() ' Add integers to Queue. Dim q As Queue(Of Integer) = New Queue(Of Integer)() q.Enqueue(5) q.Enqueue(10) q.Enqueue(15) q.Enqueue(20) ' Loop over the Queue. For Each element As Integer In q Console.WriteLine(element) Next End Sub End Module
5 10 15 20
Enum Queue. In this example, we explore how the Queue can be used to store help requests in a system. As users request help for a program, the requests can be added to the Queue with Enqueue.
Info Requests (represented by the RequestType enum) can be read with Dequeue after testing them with Peek.
Enum
Important With a Queue, the oldest (first added) help requests will be the first to be handled.
Also With a Stack, the newest help requests would be the first to be handled. You provide help to those who most recently requested it.
Module Module1 Enum RequestType As Integer MouseProblem TextProblem ScreenProblem ModemProblem End Enum Sub Main() Dim help As Queue(Of RequestType) = New Queue(Of RequestType)() ' Add some problems to the queue. help.Enqueue(RequestType.TextProblem) help.Enqueue(RequestType.ScreenProblem) ' If first problem is not a mouse problem, handle it. If help.Count > 0 Then If Not help.Peek = RequestType.MouseProblem Then ' This removes TextProblem. help.Dequeue() End If End If ' Add another problem. help.Enqueue(RequestType.ModemProblem) ' See all problems. For Each element As RequestType In help Console.WriteLine(element.ToString()) Next End Sub End Module
ScreenProblem ModemProblem
CopyTo. How can you copy a Queue to an array? Some other functions in your program may demand an array argument or you may want to simply store the elements in an array field.
Info First allocate an array with the array syntax. Call CopyTo and pass the reference variable to that array as the first argument.
Array
Array.Copy
Module Module1 Sub Main() ' New queue. Dim q As Queue(Of Integer) = New Queue(Of Integer)() q.Enqueue(5) q.Enqueue(10) q.Enqueue(20) ' Create new array of required length. Dim arr(q.Count - 1) As Integer ' CopyTo. q.CopyTo(arr, 0) ' Display elements. For Each element In arr Console.WriteLine(element) Next End Sub End Module
5 10 20
We explored some characteristics of the Queue collection. With Queue, we gain a FIFO collection implementation. This can be used to implement higher-level concepts such as a help request system.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 2, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.