如何在IF,ELSE,THEN条件下使用OR - VB.Net

时间:2014-11-19 18:38:39

标签: vb.net

尝试使用几个条件编写IF,ELSE,THEN语句。下面是我的vb.net代码:

If AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Egan Jones" Or _
        AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Farient Advisors" Or _
        AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Glass Lewis" Or _
        AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Internal Guidelines" Or _
        AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "ISS" Or _
        AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "None" Then
            RunSql("insert into institution_has_policy (institution_id,policy_id) values (" & _
                    InstitutionID & "," & NewPolicyID & ")")
End if

基本上,我试图告诉程序,如果AdvisoryFirmNameTextBox不等于条件中列出的任何名称,那么继续将新信息保存到Institution_has_Policy表中。

但是,每次运行它时,它都会运行FIRST IF条件,但跳过其他5.

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:3)

我知道这不是直接回答你的问题,但你可能想尝试更清洁,更容易阅读的内容:

Dim badNames = New List(Of String) From { "Eagan Jones", "Farient Advisors", "Glass Lewis", "Internal Guidelines", "ISS", "None" }

if (badNames.Contains(AdvisoryFirms.AdvisoryFirmNameTextBox.Text)) then
    ...
else
    ...
end if

答案 1 :(得分:2)

你正在使用OR,这意味着if()测试可以在任何条件评估为布尔值TRUE时立即挽救。 e.g。

 FALSE or FALSE or FALSE or TRUE or FALSE .... FALSE = TRUE

如果你的.Text不等于“Egan Jones”,那么那就是“真”,并且声明的其余部分变得无关紧要,执行将直接跳到'true'子句。

您想要AND

TRUE and TRUE and FALSE and TRUE ... = FALSE

答案 2 :(得分:0)

尝试OrElse!

If AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Egan Jones" OrElse _
    AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Farient Advisors" OrElse _
    AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Glass Lewis" OrElse _
    AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "Internal Guidelines" OrElse _
    AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "ISS" OrElse _
    AdvisoryFirms.AdvisoryFirmNameTextBox.Text <> "None" Then
        RunSql("insert into institution_has_policy (institution_id,policy_id) values (" & _
                InstitutionID & "," & NewPolicyID & ")")