O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Stack Class

Namespace

System.Collections

Createable

Yes

Syntax

Dim stackvariable As [New] Stack
stackvariable (required; Stack object)

The name of the Stack object

Description

A Stack object is a model of a stack.

Succinctly put, a stack is a last-in, first-out data structure. (This is often abbreviated LIFO.) Put another way, a stack is a data structure that models a stack of items (like a stack of dinner plates). There is a method for inserting items at the top of the stack (pushing) as well as a method for removing the item that is currently at the top of the stack (popping). Under this scenario, the next item to be popped is the item that was placed in line last — hence the phrase, last-in, first-out.

Note that the elements in a Stack object are of type Object.

Stack class members marked with a plus sign (+) are discussed in detail in their own entries.

Public Shared Method

Synchronized

Public Instance Properties

Count +
IsReadOnly
IsSynchronized
SyncRoot

Public Instance Methods

Clear +
Clone
Contains +
CopyTo +
Equals
GetEnumerator
GetHashCode
GetType
Peek +
Pop +
Push +
ToArray +
ToString

Example

' Define a new stack Dim s As New Stack( ) ' Push some items onto the stack s.Push("Chopin") s.Push ("Mozart") s.Push ("Beethoven") ' Is an item in the stack? MsgBox("Beethoven in stack: " & CStr(s.Contains("Beethoven"))) ' Peek at the first (top) item on the stack MsgBox("First item in stack is: " & s.Peek.ToString) ' Send stack to an array and display all items Dim s() As Object = s.ToArray( ) Dim i As ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required