
- VB.Net Basic Tutorial
- VB.Net - Home
- VB.Net - Overview
- VB.Net - Environment Setup
- VB.Net - Program Structure
- VB.Net - Basic Syntax
- VB.Net - Data Types
- VB.Net - Variables
- VB.Net - Constants
- VB.Net - Modifiers
- VB.Net - Statements
- VB.Net - Directives
- VB.Net - Operators
- VB.Net - Decision Making
- VB.Net - Loops
- VB.Net - Strings
- VB.Net - Date & Time
- VB.Net - Arrays
- VB.Net - Collections
- VB.Net - Functions
- VB.Net - Subs
- VB.Net - Classes & Objects
- VB.Net - Exception Handling
- VB.Net - File Handling
- VB.Net - Basic Controls
- VB.Net - Dialog Boxes
- VB.Net - Advanced Forms
- VB.Net - Event Handling
- VB.Net Advanced Tutorial
- VB.Net - Regular Expressions
- VB.Net - Database Access
- VB.Net - Excel Sheet
- VB.Net - Send Email
- VB.Net - XML Processing
- VB.Net - Web Programming
- VB.Net Useful Resources
- VB.Net - Quick Guide
- VB.Net - Useful Resources
- VB.Net - Discussion
VB.Net - Stack
It represents a last-in, first-out collection of objects. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item, and when you remove it, it is called popping the item.
Properties and Methods of the Stack Class
The following table lists some of the commonly used properties of the Stack class −
Sr.No | Property & Description |
---|---|
1 | Count Gets the number of elements contained in the Stack. |
The following table lists some of the commonly used methods of the Stack class −
Sr.No. | Method Name & Purpose |
---|---|
1 |
Public Overridable Sub Clear Removes all elements from the Stack. |
2 |
Public Overridable Function Contains (obj As Object) As Boolean Determines whether an element is in the Stack. |
3 |
Public Overridable Function Peek As Object Returns the object at the top of the Stack without removing it. |
4 |
Public Overridable Function Pop As Object Removes and returns the object at the top of the Stack. |
5 |
Public Overridable Sub Push (obj As Object) Inserts an object at the top of the Stack. |
6 |
Public Overridable Function ToArray As Object() Copies the Stack to a new array. |
Example
The following example demonstrates use of Stack −
Module collections Sub Main() Dim st As Stack = New Stack() st.Push("A") st.Push("M") st.Push("G") st.Push("W") Console.WriteLine("Current stack: ") Dim c As Char For Each c In st Console.Write(c + " ") Next c Console.WriteLine() st.Push("V") st.Push("H") Console.WriteLine("The next poppable value in stack: {0}", st.Peek()) Console.WriteLine("Current stack: ") For Each c In st Console.Write(c + " ") Next c Console.WriteLine() Console.WriteLine("Removing values ") st.Pop() st.Pop() st.Pop() Console.WriteLine("Current stack: ") For Each c In st Console.Write(c + " ") Next c Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following result −
Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A