Home > Back-end >  How to get a range of values - Visual basic
How to get a range of values - Visual basic

Time:11-01

I made a quiz and at the end of the quiz the user gets feedback depending on how well they got. Here is the code:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    lblScore11.Text = Val(lblScore10.Text)
    If lblScore11.Text = 100 Then 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    ElseIf lblScore11.Text = 90 Or 80 Or 70 Then 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    ElseIf lblScore11.Text = 60 Or 50 Or 40 Then 'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    ElseIf lblScore11.Text = 30 Or 20 Or 10 Then 'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Else
        lblScore11.Text = 0  'Deals if the user gets no marks
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
    End If
End Sub

If the user gets a 100 the first line of code works fine but if the user gets anything wrong, the second feedback is always given. How do I fix this?

CodePudding user response:

You should deal with the score as a numeric value, not as text.

Then you only need to check if the score is greater than or equal to the lowest score in each bracket:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    lblScore11.Text = lblScore10.Text
    Dim score As Integer = CInt(lblScore10.Text)
    If score = 100 Then 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    ElseIf score >= 70 Then 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    ElseIf score >= 40 Then 'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    ElseIf score >= 10 Then 'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Else
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
    End If
End Sub

CodePudding user response:

You should set Option Strict On. See: What do Option Strict and Option Explicit do?. If you don't, you will not get error messages when doing unwanted conversions.

You write ElseIf lblScore11.Text = 90 Or 80 Or 70 Then. This does not do what you expect, because you would have to write ElseIf lblScore11.Text = 90 Or lblScore11.Text = 80 Or lblScore11.Text = 70 Then. What it does now, is to convert 80 and 90 to True, because a Boolean is expected there. So, it does ElseIf (lblScore11.Text = 90) Or True Or True Then and this is always True. Or does it do a bitwise OR with 80 Or 70 and convert that to True to compare it to the first result. I am not sure. But it is wrong anyway.

Also, what happens if other values as e.g. 54 are involved? You are just ignoring them.

You are also comparing Strings to Integers. Convert the strings to an Integer and then do the comparisons. See @Idle_Mind's answer for a good solution.

CodePudding user response:

You should review your naming conventions. lblScore11, lblScore10 are terrible names and very prone to confusion and bugs. txtFinalFeedback makes sense.

And this code should be more straightforward. Why not fetch the value of lblScore10 directly.

For this type of construct I believe it is more elegant to use a select case statement, which could be useful also if you will be working with number ranges. Accordingly the code could be refactored like this:

Select Case score
    Case 100 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    Case Is >= 70 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    Case Is >= 40  'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    Case Is >= 10  'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Case Else
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
End Select
  • Related