Resolved Form can't see Module's Public Variable anymore.

jcardana

Old to VB6, New to VB.NET
Joined
Oct 26, 2015
Messages
72
Location
Rio Rancho, NM
Programming Experience
Beginner
It was working fine this morning. I've been having issues with defender. Now this...

In my module I have...
VB.NET:
    Public WriteToLog As StreamWriter

In my FormLoadEvent, I have...
VB.NET:
        Dim WriteToLog As New StreamWriter(UserLFM_Dir & "\~LFM_ErrorLog.txt")
It gets through the Writeline commands until I get to the FormResize...
VB.NET:
            WriteToLog.WriteLine(Format(Now, "yyyyMMdd.HHmmss") & "- " & Routine(SubLevel) & " - ENTER")

Then I get NullReference.
 
The first line is declaring a public field in your module. The second line is declaring a local variable in your form's Load event handler that is completely unrelated to the field in the module. The third line fails because you never assigned anything to that public field. The second line should not be declaring a new variable. It should be assigning to the existing field:
VB.NET:
WriteToLog = New StreamWriter(Path.Combine(UserLFM_Dir, "~LFM_ErrorLog.txt"))
Note the use of Path.Combine to build the file path:
 
BTW, WriteToLog is a bad name for that field. That sounds like the name of a method that writes to a log. Verb-based names should be used for methods while variables and properties should have noun-based names. That field should be named LogWriter.
 
Thank you for the correction and naming advice! You're awesome!
 
Done and done. While I was doing a clean up, I must have deleted something and then "corrected" it wrong.

Your instruction and advice has been implemented. I've been reading up on the naming conventions, but this is the first I've read about using nouns and verbs. Love it!
 
Back
Top