HomeSearch

VB.NET Queue Examples

This VB.NET tutorial uses the Queue generic collection. It provides syntax examples.
Queue. A Queue is a generic collection in VB.NET. It implements a FIFO algorithm. With Queue, you can keep a sliding cache of elements, with the one added first always being the first to be removed.Stack

First-In-First-Out: The element that is added first (with Enqueue) is also the first one that is removed (with Dequeue).

Enqueue. This program demonstrates two important concepts of the Queue type. First, it shows the Enqueue method, 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 Each, For
VB.NET program that uses Queue generic type 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 Output 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.

Enum: 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.

Stack: With a Stack, the newest help requests would be the first to be handled. You would always provide help to people who most recently requested it.

VB.NET program that uses Enum Queue 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 Output 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. To copy to an array, first allocate an array with the array syntax.

Next: Call CopyTo and pass the reference variable to that array as the first argument.

VB.NET program that uses CopyTo 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 Output 5 10 20
Summary. In these examples, we explored some characteristics of the Queue collection in the VB.NET language. With Queue, we gain a FIFO collection implementation. This can be used to implement higher-level concepts such as a help request system.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls