将字符串转换为整数并获得 null 等于 0 [英] Convert String to integer and get null equal to 0

查看:34
本文介绍了将字符串转换为整数并获得 null 等于 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以知道有什么简单的方法可以执行此操作吗?当 a.text 为空时,它会出错.如果不一一检测,可以用简单的代码将a.text null转为0吗?

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?

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

推荐答案

你要一一检测.更好的方法是创建自己的函数.试试下面.

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

这篇关于将字符串转换为整数并获得 null 等于 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆