知道在IF..THEN语句中哪个条件返回False

时间:2017-09-11 10:37:11

标签: vb.net if-statement

所以我有一个用户注册表单。 输入名称年龄用户名密码确认密码的文本框,安全问题回答。 在点击提交按钮之前,我想确保填写所有框并且密码匹配。

PasswordMatch返回密码是否匹配为True或False

这是我的代码:

 Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click
    'check if everything is filled
    If Name_TxtBox.Text <> "" And Age_Box.Text <> "" And username_Box.Text.Length > 4 And PasswordMatch = True And Security_Ques.Text.Length > 5 And Answer_Box.Text.Length > 2 Then
        'yupp, everything fine. now continue
    End If
End Sub

现在,我想确切地知道哪个条件是假的,所以我可以做出相应的反应。

有没有办法知道这个?喜欢捕捉异常还是什么?我知道这与此无关,但我想也许我们可以知道哪种情况是错误的。

我看到了this回答,他们建议使用ElseIf。这可能是一种方式,但这将是太冗长,需要更多代码。

2 个答案:

答案 0 :(得分:2)

如果你有一个if条件,我认为你无法确定哪个是假的,所以如果条件创建多个。

If Name_TxtBox.Text = "" Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

If Age_Box.Text = "" Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

If Not username_Box.Text.Length > 4 Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

If Not PasswordMatch Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

If Not Security_Ques.Text.Length > 5 Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

If Not Answer_Box.Text.Length > 2 Then
    'Do whatever you want
    Return 'This exits the sub, so the following lines won't execute
End If

'yupp, everything fine. now continue

答案 1 :(得分:1)

我想,你可以这样做。

If Name_TxtBox.Text <> "" Then
    If Age_Box.Text <> "" Then
    Else
       'Error: Age is empty
    End If
Else
   'Error: Name is empty
End If