Stack: Pop

Pop

Removes and returns the item at the top of the Stack.



 Public Function Pop ( ) As Variant

Return Values

Variant -  The item removed from the top of the Stack.

Remarks

This method is similar to the Peek method, but Peek does not modify the Stack.

Exceptions

ExceptionCondition
InvalidOperationException The Stack is empty.

Examples

The following example shows how to add elements to the Stack, remove elements from the Stack, or view the element at the top of the Stack.

Public Sub Main()
    ' Creates an initializes a new Stack.
    Dim MyStack As New Stack
    MyStack.Push "The"
    MyStack.Push "quick"
    MyStack.Push "brown"
    MyStack.Push "fox"
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Removes an element from the Stack.
    Debug.Print t("(Pop)\t\t") & MyStack.Pop
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Removes another element from the Stack.
    Debug.Print t("(Pop)\t\t") & MyStack.Pop
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
    
    ' Views the first element in the Stack but does not remove it.
    Debug.Print t("(Peek)\t\t") & MyStack.Peek
    
    ' Displays the Stack.
    Debug.Print "Stack values:";
    PrintValues MyStack
End Sub

Private Sub PrintValues(ByVal MyCollection As IEnumerable)
    Dim Item As Variant
    
    For Each Item In MyCollection
        Debug.Print vbTab & Item;
    Next
    
    Debug.Print
End Sub

' This example code produce the following output.
'
'    Stack values:    fox    brown    quick    The
'    (Pop)       fox
'    Stack values:    brown    quick    The
'    (Pop)       brown
'    Stack Values:    quick The
'    (Peek)      quick
'    Stack Values:    quick The

See Also

Project CorLib Overview

Class Stack Overview

Push

Peek