Jul
24
2003
VB6 // Stack

Stack Class

Store Items with this simple Stack Class

Sometimes I need to hold a few items in a stack and use the Collection Object to do this. There's no need of course, now that that Stack Class is here anyway. I've used a Variant array to hold the items so it can take strings, numbers, objects etc.

The buffer size is set to 100. If you're intending to store a lot more than 100 items in the stack (say 1000) then you'll want to adjust the buffer size to increase efficiency (as the Items array would be redimensioned 10 times).

Usage

Add StackClass.cls to your project

'Example 1

   Dim Stack as StackClass

   Set Stack = New StackClass

   Stack.Push 1
   Stack.Push 2
   Stack.Push 3

   Debug.Print Stack.Peek  'Prints 3
   Debug.Print Stack.Pop   'Prints 3
   Debug.Print Stack.Pop   'Prints 2
   Debug.Print Stack.Pop   'Prints 1

   Set Stack = Nothing


'Example 2

   Dim Stack as StackClass

   Set Stack = New StackClass

   Stack.Push 1
   Stack.Push 2
   Stack.Push 3

   While Stack.Count > 0
      Debug.Print Stack.Pop 'Prints 3, then 2, then 1
   Wend

   Set Stack = Nothing

Downloads

  StackClass.zip - contains: StackClass.cls (717 bytes)

More...