Reputation: 145
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
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
Upvotes: 1
Reputation: 218818
It looks like there are two problems here:
score
value before you calculate it.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
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