3

I already put the input for weight and height, and the message comes out. I want to make try catch for age too, its for integer, and the system knows that it is from age.

For example, I input age = "k" . then messagebox will say "wrong input for age". then when I input for weight neither height = "a" . then messagebox will say "wrong input height" or "wrong input weight" or "wrong input hiehgt and weight"

How to like that exception?

    Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
    Dim peopleName As String
    Dim peopleAge As Integer
    Dim peopleWeight As Double
    Dim peopleHeight As Double
    Dim peopleBMI As Double

    If Me.TextBoxWeight.Text <> "" Or Me.TextBoxHeight.Text <> "" Or Me.TextBoxAge.Text <> "" Then
        Try
            Me.peopleWeight = Double.Parse(Me.TextBoxWeight.Text)
            Me.peopleHeight = Double.Parse(Me.TextBoxWeight.Text)
            Me.peopleAge = Integer.Parse(Me.TextBoxAge.Text)
        Catch
            'this one is for weight/height
            MessageBox.Show("Wrong Input for Weight and Height", "Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'Catch for age is here?

        End Try

    End If
End Sub
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46

3 Answers3

2

Your approach of checking the values is not good. You can follow this idea :

    If Integer.TryParse(TextBoxAge.Text, peopleAge) = False Then
        MessageBox.Show("wrong input for age")
        Exit Sub
    End If
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
  • Thats mean, I have to do that for others (weight , height, weight and height) too? – Ticherhaz FreePalestine Feb 23 '18 at 09:31
  • 1
    Yes, avoid using Try ... Catch to verify values – Md. Suman Kabir Feb 23 '18 at 09:32
  • 1
    @Md.SumanKabir IsNumeric is often not a good choice for checking if a string can be parsed as a number, e.g. [Wrong result from IsNumeric() in VB.NET](https://stackoverflow.com/q/6770117/1115360). The TryParse methods are more reliable. – Andrew Morton Feb 23 '18 at 09:40
  • @AndrewMorton - I was not here, yes we can use `TryParse` and i updated the answer, but for checking AGE which will not be larger than 150! `IsNumeric` should be fine in this case! – Md. Suman Kabir Feb 23 '18 at 10:31
2

Catching exceptions is messy and expensive. Use Double.TryParse() and Integer.TryParse() instead.

The second parameter of TryParse() is the variable which you want to set the resulting number to if the parsing succeeds.

If Double.TryParse(Me.TextBoxWeight.Text, Me.peopleWeight) = False Then
    MessageBox.Show("Invalid weight!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

If Double.TryParse(Me.TextBoxHeight.Text, Me.peopleHeight) = False Then
    MessageBox.Show("Invalid height!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

If Integer.TryParse(Me.TextBoxAge.Text, Me.peopleAge) = False Then
    MessageBox.Show("Invalid age!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

'Do your stuff here...
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
2

In general, it is best to avoid causing exceptions if you can. There are TryParse methods for parsing strings to numbers for exactly that reason.

Also, rather than show up to three message boxes if the user doesn't enter the values as needed, you can store the messages and show them all in one box. To help the user, it is a good idea to tell them what was expected.

As a by-product of storing the error messages, it is easy to test for when valid values have been entered and so know when to process the data, something like this:

Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
    Dim peopleName As String
    Dim peopleAge As Integer
    Dim peopleWeight As Double
    Dim peopleHeight As Double
    Dim peopleBMI As Double

    Dim errorMessages As New List(Of String)

    If Not Double.TryParse(TextBoxWeight.Text, peopleWeight) Then
        errorMessages.Add("Please enter a number of kg for the weight, e.g. 60.5")
    End If

    If Not Double.TryParse(TextBoxHeight.Text, peopleHeight) Then
        errorMessages.Add("Please enter a number for the height in cm, e.g. 165")
    End If

    If Not Integer.TryParse(TextBoxAge.Text, peopleAge) Then
        errorMessages.Add("Please enter a whole number for the age, e.g. 35")
    End If

    If errorMessages.Count > 0 Then
        MessageBox.Show(String.Join(vbCrLf, errorMessages), "Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        peopleBMI = peopleWeight / (peopleHeight * peopleHeight / 10000)
        MessageBox.Show("The BMI is " & peopleBMI.ToString("N1"))
    End If

End Sub

I coded it to use a height in centimeters, take out the conversion factor of 10000 and adjust the error message if the entry will be in meters.

For full marks, you should also check for sensible values.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84