Queues in Vb.Net

In many situations we require to maintain queue for processing the elements. .Net Framework provided super mechanism to fulfill this type of requirement. That mechanism called Queue.

Queue means First In First Out (FIFO). Here I am explaining how to use Queue in Vb.Net with simple example.

Main functions of Queue are Enqueue, Dequeue and Peek

Enqueue:
This will allow you to enter element into Queue.

Syntax:
Dim que as New Queue
que.Enqueue(“element”)

Dequeue:
This will delete first entered element from Queue

Syntax:
Dim que as New Queue
que.Dequeue()

Peek:
It will provide reference for the oldest Item in the Queue.

Syntax:
Dim que as New Queue
MessageBox.Show(que.Peek())

The above statement displays oldest item (or top 1 item) in the Queue.

Here I take one simple example to explain about Queue in .Net.
The interface for this example is as shown below.

Queue

The sample code is look like below.

Public Class Form1

    Dim winQueue As New Queue
    
    Private Sub btn_enque_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                         Handles btn_enque.Click
        txt_enque.Text = txt_enque.Text.Trim
        If txt_enque.Text = "" Then
            MessageBox.Show("Please enter Element")
            txt_enque.Focus()
        Else
            MessageBox.Show(txt_enque.Text & " Element entered into Queue")

            winQueue.Enqueue(txt_enque.Text)
            txt_enque.Text = ""
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
                Handles Button1.Click
        Dim arr() As Object, i As Integer
        arr = winQueue.ToArray

        If arr.Length = 0 Then
            MessageBox.Show("No Element in the Queue")
            Exit Sub
        End If

        For i = 0 To arr.Length - 1
            MessageBox.Show(arr(i))
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
                        Handles Button2.Click
        If winQueue.Count > 0 Then
            MessageBox.Show(winQueue.Dequeue() & " Element is delete from Queue")
            If winQueue.Count > 0 Then
                MessageBox.Show(winQueue.Peek() & " is the Top 1 Element")
            Else
                MessageBox.Show("No Element in Queue to Delete")
            End If
        Else
            MessageBox.Show("No Element in Queue to Delete")
        End If
    End Sub
End Class


Here we provided one textbox to allow user to enter data in to queue and we provide the three buttons. One for submitting the data to the Queue, one for displaying data from Queue and last one is delete oldest item from the Queue.

First enter some data in textbox then click "Submit” button, data is submitted to the Queue. Now if you click the “Display Queue Elements” button, the Queue will display the data it has. If you click the “Dequeue” button, the oldest item (top most item) will be deleted from the Queue.

In this way you can effectively use the Queue concept in .Net.


Download source code here