Programming Tutorials

Stack data structure in VB.net

By: Issac in VB.net Tutorials on 2009-01-30  

In VB.net, the Stack data structure is implemented as a class in the System.Collections namespace.

To use the Stack class, you first need to import the System.Collections namespace into your project.

Here's an example of using the Stack class in VB.net:

' Create a new stack
Dim stack As New Stack()

' Push items onto the stack
stack.Push("Item 1")
stack.Push("Item 2")
stack.Push("Item 3")

' Pop an item from the stack
Dim item As Object = stack.Pop()

' Peek at the top item on the stack without removing it
Dim topItem As Object = stack.Peek()

' Check if the stack contains an item
If stack.Contains("Item 2") Then
    Console.WriteLine("Stack contains Item 2")
End If

' Get the number of items on the stack
Dim count As Integer = stack.Count()

' Clear all items from the stack
stack.Clear()

In this example, we create a new Stack object and push some items onto it. We then use the Pop method to remove an item from the top of the stack and the Peek method to peek at the top item without removing it. We also use the Contains method to check if the stack contains a certain item, the Count property to get the number of items on the stack, and the Clear method to remove all items from the stack.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)