Jurdun
Jurdun

Reputation: 145

I'm making a simple math program, however i cannot get the score to work

So far in my program, two numbers are generated and calculated by the program and saves in the AnswerTextBox.Tag. I cannot however get this score to work on screen. The validation works, just not the score counter. I mean, I've probably done the score wrong all together. Any ideas on what I can do?

Private Sub Submit_Answer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit_Answer.Click
    Dim score As Integer = 0
    ScoreTxt.Text = score

    If User_Answer_Field.Text = AnswerTextBox.Tag Then
        MsgBox("Well done!")
        score = score + 1
    Else
        MsgBox("Sorry, that is false")
    End If
End Sub

Upvotes: 0

Views: 67

Answers (3)

Steven Doggart
Steven Doggart

Reputation: 43743

You are storing the score in a local variable called score. Since it's local, the variable is recreated (and initialized to zero) each time the button is clicked. Also, since the variable is local, it's value will be inaccessible from any other method. I recommend doing the following

  1. Do some reading on variable scope
  2. Store the score in a field of the class rather than a local variable
  3. Don't display the value in the UI, until after it has been incremented

Upvotes: 1

David
David

Reputation: 218818

It looks like there are two problems here:

  1. You output the score value before you calculate it.
  2. You don't persist the score value anywhere, so you reset it with every answer.

The first one is easy, output it after it's been calculated:

score = score + 1
' later...
ScoreTxt.Text = score

The second one depends on a few things, such as where you want to persist this information, whether or not this is a web application, etc. At its simplest, if the instance of the form is always available and should be maintaining the score then you can simply make it a class-level member:

' class level...
Private score As Integer = 0

Private Sub Submit_Answer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit_Answer.Click
    ' don't re-create the value here

    If User_Answer_Field.Text = AnswerTextBox.Tag Then
        MsgBox("Well done!")
        score = score + 1
    Else
        MsgBox("Sorry, that is false")
    End If

    ScoreTxt.Text = score
End Sub

So the value stays at the class level and doesn't get re-created (and, thus, reset) every time. For larger scopes you may store the value in some kind of persistence medium outside of the application, such as a database.

Upvotes: 2

Keith
Keith

Reputation: 1321

Every time it's clicked, score is being created, set to zero, potentially incremented, and then lost at the end of the function. Save your score variable as a member variable of the class so it's not lost.

If this isn't what you're talking about, then set a breakpoint on the If statement and find out what values are being held and compared there.

You're also comparing .Text (string) to .Tag

Make sure you're comparing the same type of variable.

Upvotes: 0

Related Questions