Results 1 to 5 of 5

Thread: Saving to text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Saving to text file

    Hello,

    I'm trying to save whether the user got the answers to questions "right" or "wrong" in a list to a text file. When I try to do this, it only ends up saving one line of an answer. My code is below:

    Dim File As IO.StreamWriter
    Dim Answer As String

    For i = 0 To 10
    If AnswerCheck(i) = True Then
    Answer = "Right"
    Else
    Answer = "Wrong"
    End If

    File = New IO.StreamWriter("Answers.txt")
    File.WriteLine(Answer)
    File.Close()
    Next

    Thank you.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Saving to text file

    If you expect Answer to contain multiple instances of "Right" and/or "Wrong" then you have to add multiple instances to it. Let's say that you had an Integer variable 'x' and you assign 10 to it. If you wanted to add 5 to it so that it then equaled 15, would you do this?
    vb.net Code:
    1. x = 5
    Of course you wouldn't, because that's not adding 5 to the existing value but rather replacing the existing value with 5. Now look at your code and ask yourself whether it does what you intend. Better yet, debug your code and watch it in action. If you don't know how to debug, now is the time to learn. Set a breakpoint on the first line and then step through you code and watch the state as you go and see if it actually does what you intend.

    By the way, please use the [CODE] button on the tool bar to wrap your code snippets in formatting tags for readability.

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Saving to text file

    Quote Originally Posted by RandomAccount11 View Post
    Hello,

    I'm trying to save whether the user got the answers to questions "right" or "wrong" in a list to a text file. When I try to do this, it only ends up saving one line of an answer. My code is below:


    Code:
            Dim File As IO.StreamWriter
            Dim Answer As String
            File = New IO.StreamWriter("Answers.txt")
            For i = 0 To 10
                If AnswerCheck(i) = True Then
                    Answer = "Right"
                Else
                    Answer = "Wrong"
                End If
    
                File.WriteLine(Answer)
            Next
            File.Close()

    Thank you.
    I'm pretty sure the line File = New IO.StreamWriter("Answers.txt") is generating a brand new file each time through the For loop. So I would expect you are seeing only the result of AnswerCheck(10) present in the file once your code completes.

    If you are doing repeated file I/O like this in a loop, you are much better off in general placing the code to open and close the file outside of the loop. So the File = New IO.StreamWriter("Answers.txt") line should be just above the start of your For loop, and the File.Close() line should be just after the Next statement (see above)

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Saving to text file

    Thank you. It's now working perfectly.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Saving to text file

    I realise now that I misread your code, and that was in part because you didn't use code tags and the code wasn't indented properly. With the indenting shown in post #3, I see what I missed the first time. That's a perfect example of why formatting code snippets is important.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width