vb.net 将字符串转换为整数并获得 null 等于 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41890058/
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
Convert String to integer and get null equal to 0
提问by Mike
May I know got any simple way to perform this? It will hit an error when a.text is null. If I don't detect one by one, With a simple code may I convert a.text null to 0?
我可以知道有什么简单的方法可以执行此操作吗?当 a.text 为空时,它会出错。如果我不一一检测,用简单的代码我可以将a.text null 转换为0吗?
Dim count1 As Integer = 0
count1 = Convert.ToInt32(a.Text) + Convert.ToInt32(b.Text) + Convert.ToInt32(c.Text)
txt_display.Text = count1
Got any other method rather that I do like below detect one by one.
有任何其他方法而不是我喜欢在下面一一检测。
if a.Text = "" Then
a.Text = 0
End If
回答by Mukul Varshney
You have to detect one by one. Better way, will be to create your own function. Try below.
你要一一检测。更好的方法是创建自己的函数。下面试试。
Dim count1 As Integer = 0
count1 = ConvertToInteger(a.Text) + ConvertToInteger(b.Text) + ConvertToInteger(c.Text)
txt_display.Text = count1
Private Function ConvertToInteger(ByRef value As String) As Integer
If String.IsNullOrEmpty(value) Then
value = "0"
End If
Return Convert.ToInt32(value)
End Function
回答by FN90
A different approach using If Operator:
使用 If 运算符的另一种方法:
Dim count1 As Integer = 0
count1 = If(String.IsNullOrEmpty(a.Text), 0, Convert.ToInt32(a.Text)) + If(String.IsNullOrEmpty(b.Text), 0, Convert.ToInt32(b.Text)) + If(String.IsNullOrEmpty(c.Text), 0, Convert.ToInt32(c.Text))
txt_display.Text = count1
回答by Steve
If your objective is to sum the values in your textboxes and ignore the textboxes that cannot be converted to integers then you can simply use Int32.TryParse.
It will set your variable to 0 if the text cannot be converted to an integer without throwing exceptions.
如果您的目标是对文本框中的值求和并忽略无法转换为整数的文本框,那么您可以简单地使用Int32.TryParse。
如果文本不能在不抛出异常的情况下转换为整数,它会将您的变量设置为 0。
' In place of your textboxes
Dim x1 As String = "2"
Dim x2 As String = Nothing
Dim x3 As String = "5"
Dim a, b, c As Integer
Int32.TryParse(x1, a)
Int32.TryParse(x2, b)
Int32.TryParse(x3, c)
Dim result = a + b + c
Console.WriteLine(result)
Instead, if you want to write the "0" string into the textbox text to signal your user of the wrong input, then you have to check the textboxes one by one, again using Int32.TryParse
相反,如果您想将“0”字符串写入文本框文本以向您的用户发出错误输入信号,那么您必须再次使用 Int32.TryParse 逐个检查文本框
Dim value1 as Integer
if Not Int32.TryParse(a.Text, value1) Then
a.Text = "0"
End If
' Here the variable value1 contains the converted value or zero.
' repeat for the other textboxes involved
回答by Svekke
Example:
例子:
If String.IsNullOrEmpty(a.Text) Then
a.Text = "0"
End If
回答by Ama
As per the Microsoft Documentation, a null string (Nothing) is different from an empty string (""):
根据Microsoft 文档,空字符串 ( Nothing) 与空字符串 ( "") 不同:
The default value of String is Nothing (a null reference). Note that this is not the same as the empty string (value "").
String 的默认值为 Nothing(空引用)。请注意,这与空字符串(值“”)不同。
You also use the =operator, which can be tricky with Stringtypes. For more info, see this postand the answer which was awarded a 50 points bounty.
您还可以使用=运算符,这对于String类型来说可能很棘手。有关更多信息,请参阅此帖子和获得 50 分赏金的答案。
If you use Ifwith only two arguments, the following code
If a.Text Is Nothing Then
a.Text = 0
End If
Can be turned into a one-liner: Dim MyNumber as Integer = If(a.Text, 0)
可以变成单线: Dim MyNumber as Integer = If(a.Text, 0)
If you meant to work with empty strings, then you can use: Dim MyNumber as Integer = If(a.Text.Length = 0, 0, a.Text).
如果您打算使用空字符串,则可以使用:Dim MyNumber as Integer = If(a.Text.Length = 0, 0, a.Text)。
If you want to deal with both, you can use String.IsNullOrEmpty(a.Text)as suggested by the currently accepted answer; or you can use a.Text="", a.Text = String.Empty, or a.Text = vbNullString, which are all equal (see the post I referred to earlier)
如果您想同时处理两者,则可以String.IsNullOrEmpty(a.Text)按照当前接受的答案的建议使用;或者您可以使用a.Text="", a.Text = String.Empty, or a.Text = vbNullString,它们都是相等的(请参阅我之前提到的帖子)
Finally, note that the conversion from a Stringtype to an Integertype is made implicitly. There is no need to use the explicit cast conversions such as CType()or Convert.ToInt32.
最后,请注意从String类型到Integer类型的转换是隐式进行的。无需使用显式强制转换,例如CType()或Convert.ToInt32。

