Stack Class

   
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 Integer For i = 0 To UBound(s)     Console.WriteLine(CStr(s(i))) Next ' Clear stack s.Clear(  ) 

VB.NET/VB 6 Differences

The Stack object is new to the .NET Framework.

See Also

Collection Class, Hashtable Class, Queue Class

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net