2014-02-22 46 views
0

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 

末級

+0

不要吞下那樣的例外。除非有充分理由壓制他們,否則這是一種不好的做法。 – Steve

回答

0

如果lengtha = 79,228,162,514,264,337,593,543,950,335然後 MSGBOX( 「無法計算,嘗試更小的值。」) 結束如果

僅檢查精確匹配。我想你也想要更大的數字。 也許您可以檢查字符數是否小於30,前兩個小於79.

也請確保文化信息是正確的,因爲有些國家使用。反之亦然。

2

請停止使用IsNumeric來檢查一個字符串是否可以被視爲一個數字。
IsNumeric是VB6和its disandvantage are numerous的遺物,並且是衆所周知的。

您應該使用Decimal.TryParse檢查並將隱藏在字符串中的潛在十進制值轉換爲其十進制類型,並且不使用空try/catch來隱藏異常。
就這樣,如果你的代碼有錯誤,你將會有困難的時候去診斷它。

所以讓我嘗試了不同的方法

Private Sub Button_Click(sender As Object, e As RoutedEventArgs) Handles Calculate.Click 

    Dim lengtha As Decimal 
    Dim length As String = LengthTextBox.Text 
    If Not Decimal.TryParse(length, legtha) Then 
     MsgBox("The value entered in length is not numeric, please enter a numeric value.", MsgBoxStyle.OkOnly, "Error") 
     Return 
    End If 
    If lengtha < 0 Then 
     MsgBox("Length can not be negative.") 
     Return 
    End If 

    Dim widtha As Decimal 
    Dim width As String = WidthTextBox.Text 
    If Not Decimal.TryParse(width, widtha) Then 
     MsgBox("The value entered in width is not numeric, please enter a numeric value.", MsgBoxStyle.OkOnly, "Error") 
     Return 
    End If 
    If widtha < 0 Then 
     MsgBox("Width can not be negative.") 
     Return 
    End If 

    Try 
     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 
     MsgBox(ex.Message) 
    End Try 
End Sub 

也有兩種可能出現的問題做到心中有數。

首先,如果您的用戶類型的數字用千個分隔符爲你所顯示的,那麼你就需要使用不同版本的TryParse的,在一個允許通過一個NumberStyle枚舉和NumericFormatInfo

If Not Decimal.TryParse(length, _ 
     NumerStyles.AllowThousands Or NumberStyles.AllowDecimal, _ 
     CultureInfo.CurrentCulture, legtha) Then 

其次,執行獲取值calculations1calculations2的多重定位可能會導致值太大而無法用十進制變量表示,您可能會遇到溢出異常。如果不考慮需要輸入寬度和長度值的上下文,則難以解決此問題。也許檢查字符串的最大長度字符可以避免它

0

這可能不是很優雅的代碼,但也許你可以更好地理解它。嘗試使用Decimal.Tryparse解析字符串並採取相應措施。請參閱代碼註釋以獲取更多解釋。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    'Save input strings in easier variables 
    Dim inputLength As String = TextBox1.Text 
    Dim inputWidth As String = TextBox2.Text 

    Dim length, width As Decimal 'These will hold our results 

    'Tryparse tries to convert the string to decimal. Returns true if successful 
    'and false if not successful. If true then the result-field will also hold the 
    'converted value 
    Dim IsLengthOk As Boolean = Decimal.TryParse(inputLength, length) 
    Dim IsWidthOk As Boolean = Decimal.TryParse(inputWidth, width) 

    'Create an error message if either of the values is invalid and exit the sub if so 
    Dim ErrorString As New System.Text.StringBuilder 
    If Not IsLengthOk Then ErrorString.AppendLine("The value you entered for length is not a valid number.") 
    If Not IsWidthOk Then ErrorString.AppendLine("The value you entered for width is not a valid number.") 
    If Not IsLengthOk OrElse Not IsWidthOk Then 
     MessageBox.Show(ErrorString.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     Exit Sub 
    End If 

    'Now check the values if they are larger than zero (you could also just use 
    'Math.Abs(length) to make the numbers positive 
    IsLengthOk = (length > 0) 'assign the result of the comparison to the boolean variables 
    IsWidthOk = (width > 0) 

    'Again, create error messages if there are errors 
    ErrorString.Clear() 
    If Not IsLengthOk Then ErrorString.AppendLine("Length must be larger than zero!") 
    If Not IsWidthOk Then ErrorString.AppendLine("Width must be larger than zero!") 
    If Not IsLengthOk OrElse Not IsWidthOk Then 
     MessageBox.Show(ErrorString.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     Exit Sub 
    End If 

    'Calculate now your results and output them 
    Dim calculations1 As Decimal = length * width 
    AnswerLabel.Content = "Area: " + calculations1.ToString 
    Dim calculations2 As Decimal = length * 2 + width * 2 
    answerLabel2.Content = "Perimeter: " + calculations2.ToString 
End Sub