petrikor
petrikor

Reputation: 19

Issue converting percentage into grade - all results show as an A

This is a pretty simple project, for some reason the output always shows the results that are under Case >= 85, so an A. Could anyone help me see why? Thanks

Public Class frmMarkBook
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

        Dim intScore As Integer 'variable for the score entered

        Select Case intScore = CInt(txtScore.Text) 'converts score to an integer
            Case >= 85
                lblGradeResult.Text = ("A")
                txtGradeComment.Text = ("Excellent result")
            Case 70 To 84.99
                lblGradeResult.Text = ("B")
                txtGradeComment.Text = ("Very good result")
            Case 55 To 69.99
                lblGradeResult.Text = ("C")
                txtGradeComment.Text = ("Satisfactory result")
            Case 40 To 54.99
                lblGradeResult.Text = ("D")
                txtGradeComment.Text = ("Needs some improvements")
            Case 0 To 39.99
                lblGradeResult.Text = ("E")
                txtGradeComment.Text = ("Needs to repeat task")
            Case Else
                MessageBox.Show("Please enter a valid percentage")

        End Select

    End Sub

Upvotes: 0

Views: 39

Answers (2)

SMHasnain
SMHasnain

Reputation: 706

You can do something like this.

        Dim intScore As Integer
        intScore = CInt(txtScore.Text)
        Select Case intScore
            Case Is >= 85
                lblGradeResult.Text = ("A")
                txtGradeComment.Text = ("Excellent result")
            Case 70 To 84.99
                lblGradeResult.Text = ("B")
                txtGradeComment.Text = ("Very good result")
            Case 55 To 69.99
                lblGradeResult.Text = ("C")
                txtGradeComment.Text = ("Satisfactory result")
            Case 40 To 54.99
                lblGradeResult.Text = ("D")
                txtGradeComment.Text = ("Needs some improvements")
            Case 0 To 39.99
                lblGradeResult.Text = ("E")
                txtGradeComment.Text = ("Needs to repeat task")
            Case Else
                MessageBox.Show("Please enter a valid percentage")
        End Select

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

The very first thing you should do is turn Option Strict On, both in the project properties and in the VS options, so it's On by default for future projects. That would have told you that that code doesn't make sense by refusing to compile.

This is wrong:

Select Case intScore = CInt(txtScore.Text)

In C# that would assign a value to intScore and then evaluate that value but in VB that's a comparison, not an assignment. That is equivalent to this:

Dim bool As Boolean = (intScore = CInt(txtScore.Text))

Select Case bool

If you want an assignment then you have to actually perform an assignment, not a comparison:

intScore = CInt(txtScore.Text)

Select Case intScore

Upvotes: 2

Related Questions