2014-02-22 115 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