2017-06-11 17 views
-2

私はvb.netの小さなプログラムで作業していますが、問題には2つのテキストボックスNamesとAge(配列)があります。名前を入力して年齢がわからない場合は、配列に何も追加せずに「年齢を入力してください」と表示されますが、追加情報:文字列 ""からタイプ 'Double'への変換は無効です。 "あなたが何か問題を見ることができるかどうかを確認するために下に私のコードを入れてください。追加情報:文字列 ""からタイプ "Double"への変換は無効です。 Vb.net

PS私は初心者です。そしてありがとう。 「配列の店の Image of what I'm seeing

文字列 として 薄暗い年代薄暗い姓の文字列

として
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 
+0

「年齢」は文字列です。それを数字と比較することはできません。 – Rob

答えて

0

あなたは比較を実行する前に整数にtxtAge.Textを変換する必要があります。また、以下に示すように、あなたがConvert.toInt32(Ages) < 1 Or Convert.toInt32(Ages) > 100前にElseIf txtAge.Text = "" Thenを置くことを確認してください:文字列が空である場合、制御は次のようにはなりませんので、あなたがElseIf txtAge.Text = ""最初に挿入する必要がありますということで

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声明FormatException例外を得るでしょう。

ElseIf txtAge.Text = ""ではなく、ElseIf String.IsNullOrEmpty(txtAge.Text)を使用してください。

+0

ありがとうございました – jdavies

+0

元気ですが、配列内の最後の項目を無効な項目に置き換えました – jdavies

関連する問題