vb.net 从输入框计算,带有字母输入的错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18810783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating from inputboxes, with error message for letter input
提问by Freshman
I've been struggelig with an embarrassing and annoying little problem in visual basic (I'm, as you probbably see, a beginner). The problem is the error message system for when letters are entered instead of numbers. I get "can't convert from integer to string.
我一直在为 Visual Basic 中的一个令人尴尬和烦人的小问题而苦苦挣扎(正如你可能看到的,我是一个初学者)。问题是输入字母而不是数字时的错误消息系统。我得到“无法从整数转换为字符串。
Any help on this would be most appreciated.
对此的任何帮助将不胜感激。
Here is my code:
这是我的代码:
Dim number1, number2 As Integer
Dim sum As String
number1 = InputBox("first value:")
number2 = InputBox("second value:")
sum = number1 + number2
If IsNumeric(sum) Then
MsgBox("The sum of the numbers " & number1 & " and " & number2 & " is: " & sum)
ElseIf Not IsNumeric(sum) Then
MsgBox("You may only type numbers into the fields!, trie again")
End If
In advance, thank you :)!
在此先谢谢你:)!
采纳答案by varocarbas
You are doing the Typeconversion wrongly. Improved code:
您正在Type错误地进行转换。改进的代码:
Dim input1, input2 As String
input1 = InputBox("first value:")
input2 = InputBox("second value:")
If IsNumeric(input1) And IsNumeric(input2) Then
MsgBox("The sum of the numbers " & input1 & " and " & input2 & " is: " & (Convert.ToInt32(input1) + Convert.ToInt32(input2)).ToString())
Else
MsgBox("You may only type numbers into the fields!, try again")
End If
InputBoxreturns strings which you are implicitely converting to Integerby associating them to integer-type variables, thus you provoke an error when inputting non-numeric values. Best way to avoid problems is relying always on the right Type, as shown in the code above: the inputs are strings but IsNumerictakes precisely strings as inputs. Once the right inputs are confirmed, the conversion to the corresponding type (Integer, but you might want to rely on Decimalor Doubleto account for decimal positions) is performed and the mathematica operation performed with numeric types. Lastly, I am performing a conversion to String(just to keep this answer consistent), although bear in mind that VB.NET performs this conversion (from number to string) implicitely without any problem.
InputBox返回您通过将Integer它们关联到整数类型变量而隐式转换为的字符串,因此在输入非数字值时会引发错误。避免问题的最佳方法是始终依赖正确的类型,如上面的代码所示:输入是字符串,但IsNumeric精确地将字符串作为输入。一旦确认了正确的输入,就会执行到相应类型(Integer,但您可能希望依赖Decimal或Double考虑小数位)的转换,并使用数字类型执行 mathematica 运算。最后,我正在执行转换String(只是为了保持这个答案一致),但请记住,VB.NET 隐式执行此转换(从数字到字符串)没有任何问题。
回答by Jay
Put validation on your number boxes so that they must be numeric and not just on your sum.
将验证放在您的数字框上,以便它们必须是数字,而不仅仅是您的总和。
If Not IsNumeric(number1) Then
MsgBox("You may only type numbers into the fields!, try again")
End If
If Not IsNumeric(number2) Then
MsgBox("You may only type numbers into the fields!, try again")
End If

