如何使VB.NET的双重和整数进行2个例外

  • 本文关键字:整数 2个 VB 何使 NET vb.net
  • 更新时间 :
  • 英文 :


我已经放置了重量和高度的输入,并且消息传出。我也想尝试年龄的捕捞量,它是整数的,并且该系统知道它是从年龄开始的。

例如,i输入年龄=" k"。然后,消息框将说"年龄的错误输入"。然后,当我输入重量时,两个高度=" a"。然后,消息框将说"错误的输入高度"或"错误输入权重"或"错误输入hiehgt and Wighte"

如何喜欢这个例外?

    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

通常,最好避免引起例外。出于这个原因,有用于解析字符串到数字的TryParse方法。

另外,如果用户未根据需要输入值,而不是显示最多三个消息框,则可以存储消息并将其全部显示在一个框中。为了帮助用户,最好告诉他们预期的是什么。

作为存储错误消息的副产品,很容易测试何时输入有效值,因此知道何时处理数据,类似的内容:

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

我对其进行了编码以使用厘米的高度,取出10000的转换因子,如果条目将以米为单位,请调整错误消息。

对于完整的分数,您还应该检查明智的值。

捕获异常是混乱且昂贵的。使用 Double.TryParse() Integer.TryParse() 而不是。

TryParse()的第二个参数是您要设置结果号的变量,如果解析成功。

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...

您检查值的方法不好。您可以遵循此想法:

    If Integer.TryParse(TextBoxAge.Text, peopleAge) = False Then
        MessageBox.Show("wrong input for age")
        Exit Sub
    End If

最新更新