错误:从字符串转换""输入'整数'无效

时间:2017-01-23 09:47:16

标签: vb.net visual-studio

Before test
Before method
Test1
Before method
Test2
After method
AfterTest

我不知道如何解决此错误:

  

从字符串转换""输入'整数'无效

3 个答案:

答案 0 :(得分:2)

尝试使用Integer.Parse:

Select Case dlg
        Case Windows.Forms.DialogResult.Yes
            If TextBox12.Text = "" Then
                Dim a, b, c As Integer
                a = Integer.Parse(TextBox7.Text)
                b = Integer.Parse(TextBox8.Text) //my problem
                c = Integer.Parse(TextBox11.Text)
                TextBox12.Text = a + b - c
            End If
            If TextBox6.Text = "" Then
                TextBox6.Text = "-"
            End If

答案 1 :(得分:0)

If Not String.IsNullOrEmpty(TextBox8.Text) Then           
    b = Integer.Parse(TextBox8.Text)
End If 

您可以检查文本框是空还是null,然后使用int parse。

或者您可以使用Pikoh建议使用Int32.TryParse

Int32.TryParse Method

答案 2 :(得分:0)

您应该查看使用Integer.TryParse。使用TryParse的好处是,如果转换失败,它将不会抛出异常:

Dim a As Integer = 0
Dim b As Integer = 0
Dim c As Integer = 0

Integer.TryParse(TextBox7.Text, a)
Integer.TryParse(TextBox8.Text, b)
Integer.TryParse(TextBox11.Text, c)

TextBox12.Text = (a + b - c).ToString()

您还应该考虑设置 Option Strict On

  

将隐式数据类型转换限制为仅扩展转换,禁止后期绑定,并禁止导致Object类型的隐式类型。

从长远来看,这将有助于您编写更好的代码。

相关问题