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