Visual Basic Stack

In visual basic, Stack is useful to represent a collection of objects which stores elements in LIFO (Last in, First out) style i.e. the element which added last will be the first to come out.

 

Generally, stacks are useful when we want to access elements from the collection in last-in-first-out style and we can store a multiple nulls and duplicate values in the stack based on our requirements.

 

By using Push() and Pop() / Peek() methods, we can add or retrieve elements from the stack. Here, the Push() method is useful to add elements to the stack and the Pop() / Peek() method is useful to retrieve elements from the stack.

 

Following is the pictorial representation of stack process flow in visual basic programming language.

 

Visual Basic (VB) Stack Process Flow Diagram

Visual Basic Stack Declaration

Generally, visual basic will support both generic and non-generic type of stacks. Here, we will learn about non-generic queue collections by using System.Collections namespace so we can add elements of different data types.

 

As discussed, the collection is a class so to define a stack, we must need to declare an instance of the stack class before we perform any operations like add, delete, etc. like as shown below.

 

Dim stk As Stack = New Stack();

If you observe the above stack declaration, we created a new stack (stk) with an instance of stack class without specifying any size.

Visual Basic Stack Properties

Following are the some of commonly used properties of stack in visual basic programming language.

 

PropertyDescription
Count It will return the total number of elements in stack
IsSynchronized It is used to get a value to inidicate that an access to stack is synchronized (thread safe) or not.

Visual Basic Stack Methods

Following are the some of commonly used methods of stack to perform an operations like add, delete, etc. on elements of stack in visual basic programming language.

 

MethodDescription
Push It is used to insert an object at the top of stack.
Pop It will remove and return an object at the top of the stack.
Clear It will remove all the elements from stack.
Clone It will create a shallow copy of stack.
Contains It is used determine whether an element exists in stack or not.
Peek It is used to return a top element from the stack.

Visual Basic Stack Example

Here, we are going to use a non-generic collection of the stack so we can add elements of different data types to the stack using Push() method. Following is the example of adding and accessing the elements of the stack in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create and initialize a stack

        Dim stk As Stack = New Stack()

        stk.Push("Welcome")

        stk.Push("Tutlane")

        stk.Push(20.5F)

        stk.Push(10)

        stk.Push(Nothing)

        stk.Push(100)

        Console.WriteLine("Number of Elements in Stack: {0}", stk.Count)

        Console.WriteLine("******Stack Elements******")

        ' Access Stack Elements

        For Each item In stk

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created a new stack (stk) and added different data type elements to the stack (stk) using Push() method and we used For Each loop to iterate through the stack to get an elements from it.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Add Elements to Stack Example Result

 

If you observe the above result, we got elements from the stack in LIFO (last in, first out) order.

Visual Basic Stack Pop() Method to Access Elements

As discussed, the stack Pop() method will always remove and return a top element of the queue. Following is the example of accessing stack elements using the Pop() method in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create and initialize a stack

        Dim stk As Stack = New Stack()

        stk.Push("Welcome")

        stk.Push("Tutlane")

        stk.Push(20.5F)

        stk.Push(10)

        stk.Push(Nothing)

        stk.Push(100)

        Console.WriteLine("Number of Elements in Stack: {0}", stk.Count)

        Console.WriteLine("******Stack Elements******")

        ' Access Stack Elements

        While stk.Count > 0

            Console.WriteLine(stk.Pop())

        End While

        Console.WriteLine("Number of Elements in Stack: {0}", stk.Count)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are accessing stack elements by using Pop() method and calling a Pop method on the empty stack will throw an exception (InvalidOperation) so to avoid that situation we are checking whether the count of stack elements are greater than zero or not using while loop.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Pop Method to Access Elements Example Result

 

If you observe the above result, every time the Pop() method has removed and returned a top element of the stack that’s the reason at the end we got a count of stack elements as 0.

 

In case, if you want to remove all the elements from the stack, then you need to use the Clear() method.

Visual Basic Stack Peek() Method to Access Elements

As discussed, the stack Peek() method will always return a last (top-most) inserted element of the stack. Following is the example of accessing stack elements using Peek() method in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create and initialize a stack

        Dim stk As Stack = New Stack()

        stk.Push("Welcome")

        stk.Push("Tutlane")

        stk.Push(20.5F)

        stk.Push(10)

        stk.Push(Nothing)

        stk.Push(100)

        Console.WriteLine("Number of Elements in Stack: {0}", stk.Count)

        Console.WriteLine("******Stack Elements******")

        ' Access Stack Elements

        Console.WriteLine(stk.Peek())

        Console.WriteLine(stk.Peek())

        Console.WriteLine(stk.Peek())

        Console.WriteLine("Number of Elements in Stack: {0}", stk.Count)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are accessing stack elements by using the Peek() method.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Stack Peek Method Example Result

 

If you observe the above result, every time the Peek() method has returned a last (top-most) element of the queue without removing any element that’s the reason at the end also the stack elements count is same as starting of the stack.

Visual Basic Stack Contains() Method

In visual basic, by using stack Contains() method we can check whether an element exists in the stack or not. In case, if the element found in the stack, it will return True otherwise False.

 

Following is the example of using stack Contains() method to check whether an item exists in the stack or not in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create and initialize a stack

        Dim stk As Stack = New Stack()

        stk.Push("Welcome")

        stk.Push("Tutlane")

        stk.Push(20.5F)

        stk.Push(10)

        stk.Push(Nothing)

        stk.Push(100)

        Console.WriteLine("******Stack Example******")

        Console.WriteLine("Contains Element 4: {0}", stk.Contains(4))

        Console.WriteLine("Contains Element 100: {0}", stk.Contains(100))

        Console.WriteLine("Contains Key 'Hello': {0}", stk.Contains("Hello"))

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used Contains() method to check for particular elements exists in the stack (stk) or not.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Stack Contains() Method Example Result

Visual Basic Stack Overview

Following are the important points which needs to remember about stack in visual basic.

 

  • In visual basic, stack is useful to store a collection of objects in a LIFO (Last in, First out) style i.e. the element which added last will come out first.
  • By using the Push() method, we can add elements to the stack.
  • The Pop() method will remove and return a top most element from the stack.
  • The stack Peek() method will always return a last (top-most) inserted element of the stack  and it won’t delete any element from the stack.