user3167227
user3167227

Reputation: 23

How can I keep value the same after closing the program? VB.net

A Test score is stored and written into a text file with other user's scores and stored in a variable.

Within the program is a score form which displays the scores from all the tests for that user in the Progress form. It works after I have completed the test, but when I log out and close the program it goes back to 0 in the label.

However when I close the program and then re-run the program, the values change back to 0 and not the actual test score written to the text file.

How would I make it so that the values are kept the same when I run the program for that user?

End Sub Module was created to store Public variables Public Topic1Score As Integer

This is a code extract from the test itself

If answers(i) = questions(i, 4) And FileOpenStatusTS = False Then
    Topic1Score += 1
    TotalScore += 1
End If
Next

If yearst = "12" And classst = "A" Then
    FileOpen(1, FileName12A1, OpenMode.Append)
    FileOpenStatus12A1 = True`

Once all the details have been entered and checked, then they are written to the Teacher accounts text file

WriteLine(1, Username, Topic1Score, TotalScore)
FileClose(1)
End If

Here is the reading of the file within the progress form.

Private Sub StProgress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Debug.Assert(Not String.IsNullOrWhiteSpace(Topic1Score))
    lblTotalScore.Text = TotalScore

    LblTopic2Score.Text = "You scored " & Topic2Score & " out of 5"

    lblStName.Text = namest
    LblStSurname.Text = surnamest


    If yearst = "12" And classst = "A" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName12A1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If

    If yearst = "13" And classst = "A" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName13A1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
    If yearst = "12" And classst = "B" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName12B1, OpenMode.Input)
        While Not EOF(1) And Filefound = False
            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
    If yearst = "13" And classst = "B" Then
        Dim Filefound As Boolean
        Filefound = False
        FileOpen(1, FileName13B1, OpenMode.Input)
        While Not EOF(1) And Filefound = False


            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
        End While
    End If
End Sub`

Latest Code... Code within test... ` FileOpenStatusT = False Dim Filefound As Boolean Filefound = False FileOpen(1, Filenamet, OpenMode.Input) While Not EOF(1) And Filefound = False Input(1, Username) 'All the details are read from that account from the TeacherAccounts text file' Input(1, Password) Input(1, namet) Input(1, surnamet)

        If Username = TxtUsername.Text And Password = TxtPassword.Text Then 'If the username and account entered are valid then the user is navigated to the TeacherMenu form'
            Filefound = True
            t = Username
        End If
    End While

    If Filefound = False Then
        MsgBox("Username and Password were not a match,please try again")
    Else
        TeacherMenu.Show()
        Me.Hide()
    End If`

progress form code... If yearst = "12" And classst = "A" Then Dim Filefound As Boolean Filefound = False FileOpen(1, FileName12A1, OpenMode.Input) While Not EOF(1) And Filefound = False

            Input(1, Username) 'All the details are read from that account from the 12A1 text file'
            Input(1, Topic1Score)
            Input(1, TotalScore)
            Filefound = True
            If Username = t Then
                lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
            End If

t is a string and set as a public variable in a module.

Upvotes: 0

Views: 2921

Answers (1)

evenro
evenro

Reputation: 2646

there are 3 different options for you:

  1. Use the file system, and create a file for it (requires some work).
    If I understand correctly - that's what you did, and you have a bug in it... for getting help with debugging it - post the code.
    I think the problem in your code is that you're opening the file in append mode.
    You didn't post the read of the file, but I guess you read the first score there, but instead of writing over it everytime - you write at the end of the file.
    so your file probably looks like that:
    0
    1
    2
    and you always read the 0...
    if this is not the problem - post the read too.

  2. Use the registry to hold the variable (requires less work).

  3. Use the user area app.config to store this data.
    This area, in contradiction to the app area, can be modified by the application, and will be retained for each user separately.

I think the third is the easiest and fastest.
good luck


So I think the problem is that in the file read you don't "narrow down" the relevant user.

In this code:

   Dim Filefound As Boolean
    Filefound = False
    FileOpen(1, FileName12B1, OpenMode.Input)
    While Not EOF(1) And Filefound = False
        Input(1, Username) 'All the details are read from that account from the 12A1 text file'
        Input(1, Topic1Score)
        Input(1, TotalScore)
        Filefound = True
-------IF THE Username == t here... - the currently logged in user ... run the following line
        lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5"
-------IF the Username == the currently logged in user - break the loop
------- UNLESS - the last student record is necessary, since the grades were written in append mode

    End While

That should solve the issue.

Is it a practice system, or a "real" system?
I'm asking since it is not secure to allow access from the end client to the file that holds the user data... for practicing it is OK, but for a real system you need to change the architecture.

Upvotes: 4

Related Questions