Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

VB.Net: Special collections The Stack

From Wiki

Jump to: navigation, search

If you find yourself doing something like this in your code.

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim _Lines As New List(Of String)
  5.         Dim _MaxLines As Int16 = 30
  6.         For i = 0 To _MaxLines - 1
  7.             _Lines.Add(i)
  8.         Next
  9.         For i = _Lines.Count - 1 To 0 Step -1
  10.             Console.WriteLine(_Lines(i))
  11.         Next
  12.         Console.ReadLine()
  13.     End Sub
  14.  
  15. End Module

Then you probably nee a stack. The Stack is a Last In First Out collection (LIFO).

The above then becomes.

  1. Module Module1
  2.  
  3.     Sub Main()
  4.         Dim _Lines As New Stack(Of String)
  5.         Dim _MaxLines As Int16 = 30
  6.         For i = 0 To _MaxLines - 1
  7.             _Lines.Push(i.ToString)
  8.         Next
  9.         While _Lines.Count > 0
  10.             Console.WriteLine(_Lines.Pop)
  11.         End While
  12.         Console.ReadLine()
  13.     End Sub
  14.  
  15. End Module

765 Rating: 2.4/5 (13 votes cast)