This code, when pasted into a Class module provides text box stack functionality.
It supports the following methods and properties:
- Push
- Pull
- IsStackEmpty
- Current
It will dynamically grow the stack as data is added, and will not allow you to pull data once the stack is empty.
The stack will accept variants, which makes it polymorphic, allowing it to accept Objects, integers, dates, strings, collections, or whatevers.
The code:
Private mlngTop As Long
Private mvarStack() As Variant
Private Sub Class_Initialize()
' stack is empty (first element is zero)
mlngTop = -1
' stack will start with one element,
' it will grow if necessary
ReDim mvarStack(0) As Variant
End Sub
Public Property Get IsStackEmpty() As Boolean
IsStackEmpty = mlngTop < 0
End Property
Public Property Get Current() As Variant
If Not Me.IsStackEmpty Then
If IsObject(mvarStack(mlngTop)) Then
Set Current = mvarStack(mlngTop)
Else
Current = mvarStack(mlngTop)
End If
Else
Err.Raise 1, "VariantStack", "Stack empty"
End If
End Property
Public Sub Push(ByVal varData As Variant)
' is array big enough to accomodate stack?
If mlngTop = UBound(mvarStack) Then
ReDim Preserve mvarStack(mlngTop + 1) As Variant
End If
' add to stack
mlngTop = mlngTop + 1
' handle objects and other variable types
If IsObject(varData) Then
Set mvarStack(mlngTop) = varData
Else
mvarStack(mlngTop) = varData
End If
End Sub
Public Function Pull() As Variant
Pull = Me.Current()
mvarStack(mlngTop) = Empty ' blank element
mlngTop = mlngTop - 1
End Function


