gpt4 book ai didi

vb.net - 将 String 转换为整数并得到 null 等于 0

转载 作者:行者123 更新时间:2023-12-02 05:57:58 26 4
gpt4 key购买 nike

我可以知道有什么简单的方法可以执行此操作吗?当 a.text 为 null 时会报错。如果我不一一检测,用一个简单的代码可以将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

有任何其他方法,而不是我喜欢下面一一检测。

if a.Text = "" Then
a.Text = 0
End If

最佳答案

你得一一检测。更好的方法是创建自己的函数。请尝试以下。

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

关于vb.net - 将 String 转换为整数并得到 null 等于 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41890058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com