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
 

Stack operations (clear, count, to array) in VB and C#.

Author: Zunnair
Download Source Code : 918_Control.zip

In this simple article you will learn that how can we count and clear the elements of stack and how can we convert stack in an array by using system.collection.stack in VB and C#.

 

Read my previous stack article. It will help you to understand more.
Clear:
To clear the values of stack, stack class provides us a function named clear (). The function takes and returns nothing. It removes all the elements stored in stack.
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 = "Stack 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 = "Stack values clear"
        MessageBox.Show(Str)
    End Sub
This simple code clears the values from stack.
Count:
Stack has a fixed size which we decide at object declaration but if we want to check how many values are stored in stack for this purpose we have a property named count. Count property returns integer which shows the numbers of values stored in stack.
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("stack has " + c + " values stored in it");
        }
VB
private void btn_Count_Click(object sender, EventArgs e)
        {
            int c=0;
            c = obj.Count;
            MessageBox.Show("stack has " + c + " values stored in it");
        }
This simple code counts the values stored in stack.
To Array:
As I discussed in my previous article that stack stores object type values. So when we want to convert stack in array we will get an array of object type. For this purpose we have a function named to array (). Function takes nothing but returns the object array. Do not get confuse with reverse order of values because the value pushed at end stores at top. When the stack is converted in array the values starts converting from top. Only an array of values is returned, values do not remove from stack.
To demonstrate make a window application. Drag button and list box on form. Press button.
Now write the following code on Button click event:
C#
private void btn_ToArray_Click(object sender, EventArgs e)
        {
            if (obj.Count != 0)
            {
                object[] arr;
                arr = obj.ToArray();
                for (int i = 0; i < obj.Count; i++)
                    listBox1.Items.Add(arr[i]);
            }
            else
                MessageBox.Show("stack is empty");
       }
VB
Private Sub btn_ToArray_Click(ByVal sender As Object, ByVal e As EventArgs)
        If obj.Count <> 0 Then
           Dim arr As Object()
            arr = obj.ToArray()
            For i As Integer = 0 To obj.Count - 1
                listBox1.Items.Add(arr(i))
            Next
        Else
            MessageBox.Show("stack is empty")
        End If
    End Sub
This is simple code to convert stack into an array.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Stack Application";
            obj.Push(1);
            obj.Push(0);
            obj.Push(7);
            obj.Push(5);
            obj.Push(6);
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Stack Application"
        obj.Push(1)
        obj.Push(0)
        obj.Push(7)
        obj.Push(5)
        obj.Push(6)
    End Sub
This simple article tells that how we can push and pop values in stack using system.collection.stack in VB and C# .net 2.0.
Article Comments
Nice code. Thanks for effort.

Posted on 11/6/2009 6:27:21 AM by Ali

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Push and Pop values in stack and getting peek value of stack in VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net