-2

I'm trying to save a class to a text file and I'm getting mixed results. Half the time the last line of the add is in the file and sometimes not. I've not been able to get a consistent output to the file.

So, I added a debug to show me what was being written just prior to the StreamWriter.Write and it showed the line that I added but it doesn't show up in the file.

enter image description here

^ This line is the last line that isn't being written to the file.

Here's what my code where I save the data looks like:

  Private sub SaveMemoUsersFile()

      If _memoList is Nothing Then
        return
      End If

      Dim memofile = Path.Combine(Configuration.DataFileLocations, $"{Configuration.CompanyID}ucMemoUsers.txt")
      Const quote As String = """"
      Const comma As String = ","
      Dim both = $"{quote}{comma}{quote}"

      Using sw = New StreamWriter(memofile)
        For Each memoUsers As MemoUsers In _memoList
          Dim sb = New StringBuilder()
          sb.Append(quote)
          sb.Append(memoUsers.Initials)
          sb.Append(both)
          sb.Append(memoUsers.EmailAddress)
          sb.Append(both)
          sb.Append(memoUsers.DelinquentLetterCode)
          sb.Append(both)
          sb.Append(memoUsers.Description)
          sb.Append(quote)
          'sb.Append(vbCr)
          console.write(sb) <--- shows the last line
          sw.WriteLine(sb.ToString()) <--- but doesn't write it to the file
        Next
      End Using

    _memoList = nothing

  End sub

Anyone have any suggestions? I'm completely lost as to why this is writing to the file randomly.

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • 2
    Flush it? Why do you have `vbCr` instead of `vbCrLf` or `vbLf` or `sw.Writeline()`? Nobody uses char 13 as line separator anymore. – Jimi Oct 12 '20 at 15:59
  • I changed that but it didn't have any affect on the save. It's still doing the same thing. Shouldn't the using handle the fush? – ErocM Oct 12 '20 at 16:20
  • I hear ya. I just had my question closed because someone who was unfamiliar with the particular technology didn't understand my question said I needed to add detail. The detail was there... Any further detail would have been irrelevant and distracting. To YOUR question I wish I had an answer but it does seem related to the presence/absence of of CR, LF, or CRLF, or none of the above. (The last line often has nothing, which looks fine to humans but not to software looking for a "complete" line.) – PaulOTron2000 Oct 12 '20 at 16:45
  • It may depend on the context of the operations. `_memoList` is not passed as argument to the method, difficult to say how it's used and what uses it (or *when*). This method is out of context: it could be run on a thread, it's not clear what kind of application this is (thus the *general Context*). I'd remove that `_memoList = nothing`, move the StringBuilder declaration before the loop and `.Clear()` it after the content has been written. Flush after the loop. -- I could probably agree that clarifications could have been asked before downvoting, but is it that important? – Jimi Oct 12 '20 at 16:59

1 Answers1

0

Might as well just build your file in the stringbuilder and write it:

Private sub SaveMemoUsersFile()

      If _memoList is Nothing Then
        return
      End If

      
      Dim q = """"
      Dim b = $"{q},{q}"

      Dim sb = New StringBuilder()

      For Each memoUsers As MemoUsers In _memoList
          sb.Append(q)
          sb.Append(memoUsers.Initials).Append(b)
          sb.Append(memoUsers.EmailAddress).Append(b)
          sb.Append(memoUsers.DelinquentLetterCode).Append(b)
          sb.Append(memoUsers.Description).AppendLine(q)
      Next

      Dim memofile = Path.Combine(Configuration.DataFileLocations, $"{Configuration.CompanyID}ucMemoUsers.txt")
     
      IO.File.WriteAllText(memoFIle, sb.ToString())

      _memoList = nothing

  End sub
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Thank you! I've learned something today! I'll clean up my code although changing it to WriteAllText didn't solve my problem. Starting to think it's the class I'm bound to. – ErocM Oct 12 '20 at 17:15
  • WriteAllText is definitely reliable, and if your stringbuilder contains the text the file will too; use the debugger to verify the contents of the stringbuilder – Caius Jard Oct 12 '20 at 19:49