Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Boolean in Subroutines.

Boolean in Subroutines.

Scheduled Pinned Locked Moved Visual Basic
helpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    This code has been giving me some trouble for a while now. When the Sub is called, the Boolean showFiles is initially passed as False. However, the first time the Sub is called recursively, showFiles is changed to True. Sub DisplayDirTree(ByVal dir As String, ByVal showFiles As Boolean, Optional ByVal level As Integer = 0) ' DisplayDirTree runs through the directory and lists each folder and file in order with indentation. ' showFiles is boolean and allows the caller to show folders & files, or just the folders. ' level is an optional parameter and is used only to set the begining indent. ' TODO: This is a recursive call, fix it. 'Create a string, set it as a Short Date so we just get mm/dd/yyyy 'Since it is a String and not a date, we must run .ToString 'Then we use .Replace("/", "_") so we get mm_dd_yyyy which is a valid windows filename Dim dt As String = Date.Today.ToShortDateString.ToString.Replace("/", "_") Dim newFilename As String = "c:\Backup\Logs\" + dt + ".txt" Dim st1 As Stream = File.Open(newFilename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite) Dim sw1 As New StreamWriter(st1) sw1.WriteLine(New String("-"c, level * 2) & dir) Try If showFiles Then For Each fname As String In Directory.GetFiles(dir) sw1.WriteLine(New String(" "c, level * 2 + 2) & fname) Next End If For Each subdir As String In Directory.GetDirectories(dir) DisplayDirTree(subdir, showFiles, level + 1) Next Catch ex As Exception Console.WriteLine(ex.Message) sw1.WriteLine((ex.Message) & (ControlChars.CrLf)) End Try sw1.Close() End Sub I am rewriting all the code in this program, but that one boolean has me stuck. The calling code is DisplayDirTree(rootDir, False) The only thing I can think of is that when passing showFiles, it for some reason defaults to True. Is this natural behaviour, or am I missing something?

    E 1 Reply Last reply
    0
    • L Lost User

      This code has been giving me some trouble for a while now. When the Sub is called, the Boolean showFiles is initially passed as False. However, the first time the Sub is called recursively, showFiles is changed to True. Sub DisplayDirTree(ByVal dir As String, ByVal showFiles As Boolean, Optional ByVal level As Integer = 0) ' DisplayDirTree runs through the directory and lists each folder and file in order with indentation. ' showFiles is boolean and allows the caller to show folders & files, or just the folders. ' level is an optional parameter and is used only to set the begining indent. ' TODO: This is a recursive call, fix it. 'Create a string, set it as a Short Date so we just get mm/dd/yyyy 'Since it is a String and not a date, we must run .ToString 'Then we use .Replace("/", "_") so we get mm_dd_yyyy which is a valid windows filename Dim dt As String = Date.Today.ToShortDateString.ToString.Replace("/", "_") Dim newFilename As String = "c:\Backup\Logs\" + dt + ".txt" Dim st1 As Stream = File.Open(newFilename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite) Dim sw1 As New StreamWriter(st1) sw1.WriteLine(New String("-"c, level * 2) & dir) Try If showFiles Then For Each fname As String In Directory.GetFiles(dir) sw1.WriteLine(New String(" "c, level * 2 + 2) & fname) Next End If For Each subdir As String In Directory.GetDirectories(dir) DisplayDirTree(subdir, showFiles, level + 1) Next Catch ex As Exception Console.WriteLine(ex.Message) sw1.WriteLine((ex.Message) & (ControlChars.CrLf)) End Try sw1.Close() End Sub I am rewriting all the code in this program, but that one boolean has me stuck. The calling code is DisplayDirTree(rootDir, False) The only thing I can think of is that when passing showFiles, it for some reason defaults to True. Is this natural behaviour, or am I missing something?

      E Offline
      E Offline
      ESTAN
      wrote on last edited by
      #2

      I don't see the problem where the showFiles boolean should be true at start or where it can be changing. What you can do (not totally sure, but at least you can try) change Sub DisplayDirTree(ByVal dir As String, ByVal showFiles As Boolean, Optional ByVal level As Integer = 0) to Private Sub DisplayDirTree(ByVal dir As String, Optional ByVal showFiles As Boolean = False, Optional ByVal level As Integer = 0) So, make it private, and make the showFiles Optional to False. Let us know what it does. another thing you can do is set a break on the beginning of the sub and Watch the variable showFiles, is it set the way you want it, and while you step through your code, does it got changed?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups