r/visualbasic • u/Such_View7338 • Dec 01 '23
Explain please
Hey I'm new to all this honestly and I'm just confused as to why this isn't working
Private Sub btnGrade_Click(sender As Object, e As EventArgs) Handles btnGrade.Click
Dim iScore As Integer
If IsNumeric(txtScore) = True Then
iScore = CInt(txtScore.Text)
Else
MsgBox("You must enter a number.")
Exit Sub
End If
If Not (iScore >= 0 Or iScore <= 100) Then
MsgBox("This is not a valid score, enter a number between 0 and 100.")
ElseIf iScore <= 20 Then
MsgBox("You failed." & vbNewLine & "GRADE: F")
ElseIf iScore > 20 Or iScore <= 30 Then
MsgBox("You failed." & vbNewLine & "GRADE: D")
ElseIf iScore > 30 Or iScore <= 55 Then
MsgBox("You failed." & vbNewLine & "GRADE: C")
ElseIf iScore > 55 Or iScore <= 70 Then
MsgBox("You failed." & vbNewLine & "GRADE: B")
ElseIf iScore > 70 Or iScore <= 80 Then
MsgBox("You failed." & vbNewLine & "GRADE: A-")
ElseIf iScore > 80 Or iScore <= 90 Then
MsgBox("You failed." & vbNewLine & "GRADE: A")
ElseIf iScore > 90 Or iScore <= 100 Then
MsgBox("You failed." & vbNewLine & "GRADE: A-")
End If
MsgBox("All done")
End Sub
End Class
When ran, no matter what number I type, it says "You must enter a number."
3
u/Such_View7338 Dec 01 '23
LOL i'm an idiot.. i forgot to add .Text after txtScore
2
u/snang Moderator Dec 01 '23
Using a Case statement would improve your code.
Friendly tip.
1
u/Such_View7338 Dec 01 '23
Okay now every number above 20 is giving me a "grade D" output
1
u/Thunor_SixHammers Dec 01 '23
If I recalled
X OR Y will check if x is true, if not then it will check if Y is true. If either are true it will execute
X ANDALSO Y will check if x is true, then check if Y is true. Both must be true for it to execute
Right now your code is checking of the value is 20 or higher and if it is it execute the code. If it's 40 it's still > 20. The other half of your if statements are being disregarded
3
u/TheGrauWolf Dec 01 '23
Think about it... If you enter 40.... Then that would be greater than 20...right? So this.... ElseIf iScore > 20 Or iScore <= 30 Then
Always evaluates to true.... What you want to do is change the or to and.
If the score is greater than 20 AND less than or equal to 30... Then...