Skip to content

vgrstn/vba-stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vba-stack

License: MIT Platform Architecture Rubberduck

Generic VBA Class based on an encapsulated VB Collection

A lightweight, Collection-backed LIFO stack for VBA with:

  • O(1) push/pop at the top (stored at position 1)
  • Safe enumeration (For Each) via a proper COM enumerator
  • Clear error semantics (Peek / Pop on empty stack raise error 5)
  • Zero dependencies (pure VBA)

📦 Features

  • Fast push/pop (top is position 1 to avoid VB Collection tail-removal penalty)
  • Enumeration: For Each item In Stack (via hidden [_NewEnum])
  • Utility export: Items([base]) returns a 0- or 1-based array copy
  • Pure VBA, no external references, Rubberduck-friendly annotations

⚙️ Public Interface

Member Type Description
Push(Item) Sub Adds an item at the top of the stack.
Pop() Function Returns and removes the top item. Raises error 5 if empty.
Peek (Default) Property Returns the top item without removing it. Raises error 5 if empty.
Count Property Number of items.
IsEmpty Property True if empty, else False.
Clear Sub Removes all items.
Items([base]) Function Returns all items as a Variant() array; base can be 0 or 1.
For Each Enumerator Iterates top → bottom (don’t mutate during enumeration).

Error behavior

  • Empty stack on Peek / Pop raises vbErrorInvalidProcedureCall (=5) with source "Stack.Peek" or "Stack.Pop".

🚀 Quick Start

Dim s As New Stack

s.Push "alpha"
s.Push "beta"
Debug.Print s.Peek        ' -> beta  (top)
Debug.Print s.Pop         ' -> beta  (removed)
Debug.Print s.Pop         ' -> alpha (removed)
Debug.Print s.IsEmpty     ' -> True

About

Generic VBA Class based on an encapsulated VB Collection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages