首页 文章

附加信息:从字符串“”到类型'Double'的转换无效 . Vb.net

提问于
浏览
-2

我正在研究vb.net上的一个小程序,问题是我有两个文本框名称和年龄(这是数组) . 并且我正在努力尝试使它如果输入一个名字并且年龄不是它将提出“请输入年龄”,而不向数组添加任何内容,这些已尝试但仍然会出现“附加信息:从字符串“”到“Double”类型的转换无效“ . 我把我的代码放在下面,看看你是否能看到任何问题 .

PS我只是一个初学者 . 谢谢你Image of what I'm seeing

'在数组中存储'Dim Ages As String Dim Surnames As String

Surnames = txtNames.Text
    Ages = txtAge.Text


    If txtNames.Text = "" Then

        MsgBox("Enter Name ")
        txtNames.Focus()
        SystemValueAge = SystemValueAge - 1
        SystemValueName = SystemValueName - 1
        txtNames.Focus()

    ElseIf Ages < 1 Or Ages > 100 Then

        MsgBox("Please Enter Valid Age")

        SystemValueAge = SystemValueAge - 1
        SystemValueName = SystemValueName - 1
        txtAge.Focus()

    ElseIf txtAge.Text = "" Then

        MsgBox("Enter Age")
        SystemValueAge = SystemValueAge - 1
        SystemValueName = SystemValueName - 1
        txtAge.Focus()


    End If

1 回答

  • 0

    在执行比较之前,您需要将 txtAge.Text 转换为整数 . 另外,请确保将 ElseIf txtAge.Text = "" Then 放在 Convert.toInt32(Ages) < 1 Or Convert.toInt32(Ages) > 100 之前,如下所示:

    ElseIf txtAge.Text = "" Then
        MsgBox("Enter Age")
        SystemValueAge = SystemValueAge - 1
        SystemValueName = SystemValueName - 1
        txtAge.Focus()
    
    ElseIf Convert.toInt32(Ages) < 1 Or Convert.toInt32(Ages) > 100 Then
        MsgBox("Please Enter Valid Age")
        SystemValueAge = SystemValueAge - 1
        SystemValueName = SystemValueName - 1
        txtAge.Focus()
    End If
    

    首先需要插入 ElseIf txtAge.Text = "" 的原因是因为如果字符串为空,那么控件将不会进入以下 ElseIf 语句,因此您将不会得到 FormatException 异常 .

    作为旁注,您应该使用 ElseIf String.IsNullOrEmpty(txtAge.Text) 而不是 ElseIf txtAge.Text = "" .

相关问题