VB.NET Stack Type

Stack illustration

You want to see the basics of the Stack collection in the VB.NET language. With a Stack generic type, elements are pushed to the top, and then popped from the top. We see how to loop over a Stack, push and pop elements, and also peek and count.

LIFO: The last element added to the Stack is the first element removed from the Stack. Thus, all the activity occurs on the top of the stack. (Elements stack up.)

Example 1

This example shows how you can Push elements to the top of a Stack repeatedly. Then, we use the For Each looping construct to display the Stack in the order from first to last. You can see that the most recently added element (1000) is at the top of the Stack.

Program that uses Stack [VB.NET]

Module Module1
    Sub Main()
	Dim stack As Stack(Of Integer) = New Stack(Of Integer)
	stack.Push(10)
	stack.Push(100)
	stack.Push(1000)

	' Display stack elements.
	For Each value As Integer In stack
	    Console.WriteLine(value)
	Next
    End Sub
End Module

Output

1000
100
10

Example 2

This example introduces the Pop and Peek functions on the Stack. With Pop, you take the top element off of the Stack and it is stored in the return value. It no longer is part of the Stack. With Peek, you take the top element and copy it to the return value, but it is not removed. To avoid exceptions with an empty stack, use the Count property and test it against zero.

Program that uses Pop and Peek [VB.NET]

Module Module1
    Sub Main()
	Dim stack As Stack(Of Integer) = New Stack(Of Integer)
	stack.Push(10)
	stack.Push(100)
	stack.Push(1000)

	' Test Pop.
	Dim value As Integer = stack.Pop
	Console.WriteLine("Element popped: {0}", value)

	' Test Peek.
	value = stack.Peek
	Console.WriteLine("Element peeked: {0}", value)
    End Sub
End Module

Output

Element popped: 1000
Element peeked: 100

Example 3

True keyword

In this example, we show how you can copy an array into a new Stack. Please note that the types of the two collections must match: the array of Strings must be copied to a Stack of Strings. Next, we use the Contains function, which works in much the same way as the List Contains function. It returns True or False.

Program that uses Stack constructor and Contains [VB.NET]

Module Module1
    Sub Main()
	Dim array(2) As String
	array(0) = "dot"
	array(1) = "net"
	array(2) = "perls"

	' Copy array to a new stack.
	Dim stack As Stack(Of String) = New Stack(Of String)(array)

	' Use Contains.
	Console.WriteLine(stack.Contains("net"))
	Console.WriteLine(stack.Contains("not"))
    End Sub
End Module

Output

True
False

Summary

The VB.NET programming language

The Stack type can be used to implement certain parsers that track nesting levels, such as HTML parsers. As each nested element is encountered, a new element is added to the Stack. Stacks are useful in any system where the most recently added element is the most urgent; this means elements not dealt with become less important with time. The VB.NET Stack type, then, can be used to implement a variety of important algorithms.

VB.NET Tutorials
.NET