Feyisayo Sonubi
Feyisayo Sonubi

Reputation: 1072

Any Faster or Shorter way of doing this in vb?

I have an app that checks if a user's input is within a text range from A - F, my code works fine but I just thought there should be a shorter way of doing this, in one line probably?

Here's the code:

Private Sub ResultButton_Click(sender As Object, e As EventArgs) Handles ResultButton.Click

        Dim Range_A As Integer, Range_B As Integer, Range_C As Integer,
            Range_D As Integer, Range_E As Integer, Range_F As Integer, Range_Check As Integer

        Range_A = Integer.Parse(inputTextBox.Text)
        Range_B = Integer.Parse(inputTextBox.Text)
        Range_C = Integer.Parse(inputTextBox.Text)
        Range_D = Integer.Parse(inputTextBox.Text)
        Range_E = Integer.Parse(inputTextBox.Text)
        Range_F = Integer.Parse(inputTextBox.Text)

        Range_Check = Integer.Parse(inputTextBox.Text)

        If Range_Check >= 101 Or Range_Check < 0 Then

            MessageBox.Show(" Only values within 0 and 100 are allowed, please type a number within this range.")

        End If


        If Range_F >= 0 And Range_F <= 39 Then

            MessageBox.Show(" " & Range_F & " is within the F Range")
            Exit Sub

        End If

        If Range_E >= 40 And Range_E <= 44 Then

            MessageBox.Show(" " & Range_E & " is within the E Range")
            Exit Sub

        End If

        If Range_D >= 45 And Range_D <= 49 Then

            MessageBox.Show(" " & Range_D & " is within the D Range")
            Exit Sub

        End If

        If Range_C >= 50 And Range_C <= 59 Then

            MessageBox.Show(" " & Range_C & " is within the C Range")
            Exit Sub

        End If

        If Range_B >= 60 And Range_B <= 69 Then

            MessageBox.Show(" " & Range_B & " is within the B Range")
            Exit Sub
        End If

        If Range_A >= 70 And Range_A <= 100 Then

            MessageBox.Show(" " & Range_A & " is within the A Range")
            Exit Sub

        End If


    End Sub

I'm repeating the same Integer.Parse statements which last time I checked isn't always a good idea in progamming, how do I optimise these lines of code please?

Upvotes: 0

Views: 78

Answers (3)

  Dim range As String = ""
  If Not Integer.TryParse(inputTextBox.Text, theValue) Then
        MessageBox.Show("Enter only numerals, please")
        Exit Sub
  End if

  ' if integer,.tryparse is true then theValue will have the result

  Select Case theValue
      Case Is > 100, Is < 1
          ' do err msg

      Case 0 to 39
         range = "A"

      Case 40 To 50
         range = "B"

     ' etc...or:

      Case 41 to 44
            MessageBox.Show(" " & inputTextBox.Text & " is within the E Range")
  End Select

2 variables, 1 parse or just evaluate the value and display you message as in the last one. the main reason to save these to a variable would be if you needed the result elsewhere

Upvotes: 1

Deja Vu
Deja Vu

Reputation: 413

    Select Case Integer.Parse(inputTextBox.Text)
        Case 70 To 100
            MessageBox.Show("Your Grade is A")
        Case Is > 60
            MessageBox.Show("Your Grade is B")
        Case Is > 50
            MessageBox.Show("Your Grade is C")
        Case Is > 45
            MessageBox.Show("Your Grade is D")
        Case Is > 40
            MessageBox.Show("Your Grade is E")
        Case Is > 0
            MessageBox.Show("Your Grade is F")
        Case Else
            MessageBox.Show("Only values within 0 and 100 are allowed, please type a number within this range.")
    End Select

Upvotes: 2

Chris Bogart
Chris Bogart

Reputation: 472

You can just parse the input text box once, and replace every instance of Range_A, Range_B, Range_Check etc. with just plain Range, so:

Dim Range As Integer

Range = Integer.Parse(inputTextBox.Text)

If Range >= 101 Or Range  < 0 Then
        MessageBox.Show(" Only values within 0 and 100 are allowed, please type a number within this range.")
End If

If Range >= 0 And Range <= 39 Then
        MessageBox.Show(" " & Range  & " is within the F Range")
End If

... and so on for all the checks. Having separate names for these does not do anything.

Upvotes: 1

Related Questions