【问题标题】:Convert String to integer and get null equal to 0将字符串转换为整数并获得等于 0 的 null
【发布时间】:2017-06-12 21:41:44
【问题描述】:

我可以知道有什么简单的方法来执行此操作吗?当 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

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

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

【问题讨论】:

    标签: vb.net type-conversion


    【解决方案1】:

    您必须一一检测。更好的方法,将是创建自己的功能。试试下面。

    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
    

    【讨论】:

      【解决方案2】:

      如果您的目标是对文本框中的值求和并忽略无法转换为整数的文本框,那么您可以简单地使用 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)
      

      相反,如果您想将“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
      

      【讨论】:

        【解决方案3】:

        使用 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
        

        【讨论】:

          【解决方案4】:

          例子:

          If String.IsNullOrEmpty(a.Text) Then
           a.Text = "0"
          End If
          

          【讨论】:

            【解决方案5】:

            根据Microsoft Documentation,空字符串(Nothing)与空字符串("")不同:

            String 的默认值为 Nothing(空引用)。请注意,这与空字符串(值“”)不同。

            您还可以使用 = 运算符,这对于 String 类型可能会很棘手。有关更多信息,请参阅this post 以及获得 50 分赏金的答案。

            如果你使用Ifwith only two arguments,如下代码

            If a.Text Is Nothing Then
                a.Text = 0
            End If
            

            可以变成单行:Dim MyNumber as Integer = If(a.Text, 0)

            如果您打算使用空字符串,那么您可以使用:Dim MyNumber as Integer = If(a.Text.Length = 0, 0, a.Text)

            如果您想同时处理这两种情况,可以按照当前接受的答案的建议使用String.IsNullOrEmpty(a.Text);或者您可以使用a.Text=""a.Text = String.Emptya.Text = vbNullString,它们都是相等的(参见post I referred to earlier

            最后,请注意从String 类型到Integer 类型的转换是隐式进行的。无需使用CType()Convert.ToInt32 等显式强制转换。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-09-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-06-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多