Stack: Peek |
Returns the item at the top of the Stack without removing it.
Public Function Peek ( ) As Variant
This method is similar to the Pop method, but Peek does not modify the Stack.
Exception | Condition |
---|---|
InvalidOperationException | The Stack is empty. |
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