Name

Stack Class

Namespace

System.Collections

Createable

Yes

Syntax

Dimstackvariable As [New] Stack
stackvariable

Use: Required

Data Type: 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 (+) ae 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 ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.