Visual Studios的帮助指南指出,它存储在小数点中的最大可能值为79,228,162,514,264,337,593,543,950,335每当我放入大量的程序时,程序什么都不做,所以我想为用户显示一条错误消息,询问他们输入一个较小的值或以某种方式检测溢出。我试过使用十进制溢出VB 2012
If lengtha = 79,228,162,514,264,337,593,543,950,335 Then
MsgBox("Can not compute, try a smaller value.")
End If
但它没有解决。这是目前的代码。
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles Calculate.Click
Dim length As String = LengthTextBox.Text
If IsNumeric(length) Then
Else
MsgBox("The value entered in legnth is not numeric, please enter a numeric value.", MsgBoxStyle.OkOnly, "Error")
End If
Dim width As String = WidthTextBox.Text
If IsNumeric(width) Then
Else
MsgBox("The value entered in width is not numeric, please enter a numeric value.", MsgBoxStyle.OkOnly, "Error")
End If
Try
Dim lengtha As Decimal = Decimal.Parse(length)
If lengtha < 0 Then
MsgBox("Length can not be negative.")
Return
End If
Dim widtha As Decimal = Decimal.Parse(width)
If widtha < 0 Then
MsgBox("Width can not be negative.")
Return
End If
Dim calculations1 As Decimal = lengtha * widtha
AnswerLabel.Content = "Area: " + calculations1.ToString
Dim calculations2 As Decimal = lengtha * 2 + widtha * 2
answerLabel2.Content = "Perimeter: " + calculations2.ToString
Catch ex As Exception
End Try
End Sub
末级
不要吞下那样的例外。除非有充分理由压制他们,否则这是一种不好的做法。 – Steve