Tim Hastings - NonHostile (because there's no need)

Weblog and collection of geeky articles.

  Home :: Who? :: Contact :: Links :: Subscribe subscribe
Sarah Parr and Andy Stagles' Wedding Day, 22nd December 2005Good Friday at Stanley ParkMore Cybershot photies (Zoom capabilities of DSC H1)


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

 



1 comment, Visual Basic 6, Thursday, May 20, 2004 12:57

Timeline Navigation for Visual Basic 6 posts
VB6: How To Convert UTF-8 Byte Arrays into Unicode Strings (and vice versa) (made 88 weeks later)
VB6: Variant Stack Class (Code Library) (this post, made Thursday, May 20, 2004 12:57)
VB6: XML and How To Read It With Visual Basic (made 4 days earlier)


Comments
I have copy it and run it but it doesn't seem to be work...how?

Posted by: Andy on Sunday, October 25, 2009 09:00

Post a Comment
Name:  Home page and email address are optional.
  Email addresses will not be displayed or spammed!
Remember these details
Email:
Home Page:
Comment:
Comments cannot contain HTML, URLs will be formatted into hyperlinks.
I reserve the right to remove any comments for any reason.