Boolean in Subroutines.
-
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? -
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?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?