When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, April 19, 2000

Building a Stack Class Using VBScript, Part 2

  • Read Part 1

  • In Part 1 we looked at what a stack data structure is, exactly, and what semantics and methods it needs to include. Also, we looked at creating a WeakList class using the DynamicArray class presented in an earlier article (Dynamic Arrays). In this part, we will look at the code for our stack class.

    - continued -

    Recall that a stack needs to implement the following functions: pop and push. To make our stack class even more useful, we'll also add a Count property and a peek method. (The peek method will return the value at the top of the stack without altering the stack in any way.) Our stack class will contain a single member variable, an instance of the WeakList class. The event handlers of the class simple instantiate the WeakList class and clean up afterwards.

    <!--#include file="WeakList.Class.asp"-->
    Class Stack
      '********* MEMBER VARIABLES **********
      Private wlList
      '********* EVENT HANDLERS ************
      Private Sub Class_Initialize()
        'Allocate the weak list instance
        Set wlList = New WeakList
      End Sub
      Private Sub Class_Terminate()
        Set wlList = Nothing       'Clean up!
      End Sub
      '... methods and properties omitted for brevity ...
    End Class

    Note that the WeakList class definition, which was stored in a file named WeakList.Class.asp was imported via a server-side include.

    The member variable wlList serves as an instance of the WeakList class. All of the methods and properties of the stack class will use this single member variable. By doing so, the code for the stack class is short and easy to the point of lunacy.

    Following is the methods and properties for the stack class:

    <!--#include file="WeakList.Class.asp"-->
    Class Stack
      '... The member variable and event handler sections
           have been omitted for brevity ...
      '************ PROPERTIES *************
      Public Property Get Count()
        Count = wlList.Count
      End Property
      '************* METHODS ***************
      Public Function Push(varItem)
      End Function
      Public Function Pop()
        Pop = wlList.RemoveHead()
      End Function
      Public Function Peek()
         Peek = wlList.PeekHead()
      End Function
    End Class

    Pretty easy, eh? All of our methods and properties are only one line long! Neat-o. This class definition should be placed in its own file, Stack.Class.asp, for example. How about an example of using the stack class? You can also view an on-line demo of the code displayed below.

    <% Option Explicit %>
    <!--#include file="Stack.Class.asp"-->
      Dim objStack
      Set objStack = new Stack
      'Push some data on the stack
      objStack.Push("This ")
      objStack.Push("is ")
      objStack.Push("a ")
      objStack.Push("test! ")
      'What is on the top?
      Response.Write objStack.Peek() & " is on the top of the stack.<P>"
      'Remove all the data displaying it
      Response.Write "<B>Outputting the stack contents:</B><BR>"
      While objStack.Count > 0
        Response.Write objStack.Pop() & "<BR>"
      set objStack = nothing

    Well, that wraps it up! Hopefully you found this stack class and/or WeakList useful. Feel free to use these classes in your ASP projects.

    Happy Programming!

  • Read Part 1


  • Download the source to WeakList.Class.asp in text format
  • Download the source to Stack.Class.asp in text format
  • Try out the on-line Stack class demo!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article