将字符串转换为整数并获取等于 0 的 null

2024-03-08

我可以知道有什么简单的方法来执行此操作吗?当 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将字符串转换为整数并获取等于 0 的 null 的相关文章

随机推荐