Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Queue operations (clear, count, Trim, find, Copy to array) in VB and C#.

Author: Zunnair
Download Source Code : 914_Queue.zip

In this simple article you will learn that how can we count and clear the elements of queue, how can we copy queue in an array, how to trim queue to stored values size and how to find a value in a que

 

Read my previous queue article. It will help you to understand more.
Clear:
To clear the values of queue, queue class provides us a function named clear (). The function takes and returns nothing. It removes all the elements stored in queue.
To demonstrate make a window application. Drag one button on form. Press button to clear values.
Now write the following code on Button click event:
C#
private void txt_Clear_Click(object sender, EventArgs e)
        {
            obj.Clear();
            string str = "Queue values clear";
            MessageBox.Show(str);
        }
VB
Private Sub txt_Clear_Click(ByVal sender As Object, ByVal e As EventArgs)
        obj.Clear()
        Dim str As String = "Queue values clear"
        MessageBox.Show(Str)
    End Sub
This simple code clears the values from queue.
Count:
Queue has a fixed size which we decide at object declaration but if we want to check how many values are stored in queue for this purpose we have a property named count. Count property returns integer which shows the numbers of values stored in queue.
To demonstrate make a window application. Drag one button on form. Press button to count values.
Now write the following code on Button click event:
C#
private void btn_Count_Click(object sender, EventArgs e)
        {
            int c=0;
            c = obj.Count;
            MessageBox.Show("queue has " + c + " values stored in it");
        }
VB
private void btn_Count_Click(object sender, EventArgs e)
        {
            int c=0;
            c = obj.Count;
            MessageBox.Show("queue has " + c + " values stored in it");
        }
This simple code counts the values stored in queue.
Trim:
Queue has a size which we decide at object declaration time but if we want to reduce the size according to the number of values stored in queue, for this purpose we have a function named trim to size (). Function takes and returns nothing but trims the size of queue.
To demonstrate make a window application. Drag button. Press button.
Now write the following code on Button click event:
C#
private void btn_trim_Click(object sender, EventArgs e)
        {
            obj.TrimToSize();
            int c = obj.Count;
            string str = c.ToString();
            MessageBox.Show("queue trimed and size is" + str);
        }
VB
Private Sub btn_trim_Click(ByVal sender As Object, ByVal e As EventArgs)
        obj.TrimToSize()
        Dim c As Integer = obj.Count
        Dim str As String = c.ToString()
        MessageBox.Show("queue trimed and size is" & str)
    End Sub
This simple code trims queue size.
 
Copy:
If we want to copy queue in an array for this purpose we have a function named copy to (). Function takes object of array class and index which tells that what is the starting index of array at which copy should start. This function returns nothing.
To demonstrate make a window application. Drag button and list box. Press button.
Now write the following code on Button click event:
C#
private void btn_copy_Click(object sender, EventArgs e)
        {
            Array arr = new int[5];
            obj.CopyTo(arr, 0);
            MessageBox.Show("queue copied");
            for(int i=0;i
            listBox1.Items.Add(arr.GetValue(i));
        }
VB
Private Sub btn_copy_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim arr As Array = New Integer(4) {}
        obj.CopyTo(arr, 0)
        MessageBox.Show("queue copied")
        For i As Integer = 0 To arr.Length - 1
            listBox1.Items.Add(arr.GetValue(i))
        Next
    End Sub
This simple code copies the queue in an array.
Find:
If we want to find some value in queue for this purpose we have a function named contains (). Function takes object value and returns Boolean. If true means value found else not found.
To demonstrate make a window application. Drag button and text box. Write value in text box and press button.
Now write the following code on Button click event:
C#
private void btn_Contains_Click(object sender, EventArgs e)
        {
            string str = txt_Contain.Text;
            int v = Convert.ToInt16(str);
            bool b;
            object ob;
            ob = (object)v;
            b = obj.Contains(ob);
            str = b.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub btn_Contains_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim str As String = txt_Contain.Text
        Dim v As Integer = Convert.ToInt16(str)
        Dim b As Boolean
        Dim ob As Object
        ob = DirectCast(v, Object)
        b = obj.Contains(ob)
        str = b.ToString()
        MessageBox.Show(str)
    End Sub
This simple code finds value in queue.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Queue Application";
            obj.Enqueue(1);
            obj.Enqueue(10);
            obj.Enqueue(100);
            obj.Enqueue(1000);
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Queue Application"
        obj.Enqueue(1)
        obj.Enqueue(10)
        obj.Enqueue(100)
        obj.Enqueue(1000)
    End Sub
This simple article tells that how can we count and clear the elements of queue, how can we copy queue in an array, how to trim queue to stored values size and how to find a value in a queue by using system.collection.queue in VB and C#.
 
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Enqueue and Dequeue values in queue, getting peek value of queue and converting queue in to array in VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net