VB.NET Queue Examples

The VB.NET programming language

You want to use the Queue generic type in your VB.NET program, and gain an understanding of the FIFO algorithm it implements. With Queue, you can keep a sliding cache of elements, with the one added first always being the first to be removed.

This VB tutorial uses the Queue generic collection. It provides syntax examples.

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

Enqueue and loop

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 shows how you can loop over the elements in the Queue with the For Each looping construct. For Each is implemented in a special way so that it shows all the internal elements in the Queue.

For Each Loop Examples
Program that uses Queue generic type [VB.NET]

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 example

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. Then, those requests (represented by the RequestType Enum) can be read with Dequeue after testing them with Peek.

Program that uses Enum Queue [VB.NET]

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

FIFO. With a Queue, the oldest (first added) help requests will be the first to be handled. If you were to use a Stack, the newest help requests would be the first to be handled. This means that you would always provide help to people that requested it most recently, making your oldest and most important customers very unhappy.

CopyTo

Question and answer

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.

Program that uses CopyTo [VB.NET]

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

Note

In these examples, we explored some characteristics of the Queue collection in the VB.NET language. With Queue, we gain a FIFO collection implementation, which can be used to implement higher-level concepts such as a help request system, where the oldest entries are handled as soon as possible.

VB.NET Tutorials
.NET