Using Collections.Stack
 Sub Main()
      Dim dxStack As New System.Collections.Stack
      Dim i1 As Integer = 5
      Dim b As Boolean = True
      Dim s As String = "cat"
      Dim l As Long = 87 ^ 5
      Dim d As Double = 33.4567
      Dim sng As Single = 1.25
      Dim ar() As String = {5, 3, 7}
      Dim o As Object
      Try
         dxStack.Push(b)
         dxStack.Push(s)
         dxStack.Push(l)
         dxStack.Push(sng)
         dxStack.Push(ar)
         dxStack.Push(d)
         dxStack.Push(i1)
         Do While dxStack.Count <> 0
            o = dxStack.Pop ' take each item off the stack
            Dim t As System.Type
            t = o.GetType
            Stop ' take a look in locals for t.Fullname etc
         Loop
      Catch ef As FormatException
         MsgBox(ef.Message)
      Catch ee As Exception
         MsgBox(ee.Message)
      End Try
 End Sub
 

The Stack is opposite the Queue. Things are put on the stack in the first-in last-out fashion. It resembles a stack of plates at a cafeteria which are put on a spring-loaded plate holder. It is one of the most important concepts in computer science. With low level machine code if you call a method like: MyMethod(a as integer, b as double), a and b would get pushed to the stack.

The stack is just a portion of memory that is assigned to be used as such. Usually it it just regular memory, a stack pointer always points to the top of the stack. If something gets popped from the stack then the stack pointer would point to the next memory location, which is nothing more than a place in memory. Sometimes the push to the stack would be used to merely save the data that was "pushed" so it could be retrieved "popped" later and restored. Otherwise a method call pushes all the data to a stack so that the method has access to them.

We don't use the stack that way in high-level programming but using a stack can be very practical at times. For example, instead of creating a bunch of different types to temporarily hold some data, you can create a stack and push all the data to it and the retrieve it later, because the stack converts everything to type object. It is very important to remember the order in which things are pushed though. If you push say, integer, integer, double to the stack but then pop it back, the first thing popped will be the double, and if you forget that and try to assign the popped value to integer an exception will be raised.

10/04/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home ArrayList 1 ArrayList 2 Queues Stacks