2017-06-02 68 views
0

我是VB.net的新手,我對這個語句沒有什麼問題,我想僅僅在txtBox中拋出msgBox txtTitul不是「Bc」。或「」或「Ing」。 ... 這是扔MSGBOX每次Visual Basic OrElse仍然返回false

If Not txtTitul.Text.Equals("Bc.") _ 
    OrElse Not txtTitul.Text.Equals("") _ 
    OrElse Not txtTitul.Text.Equals("Bc.") _ 
    OrElse Not txtTitul.Text.Equals("Mgr.") _ 
    OrElse Not txtTitul.Text.Equals("Ing.") _ 
    OrElse Not txtTitul.Text.Equals("Mgr. art.") _ 
    OrElse Not txtTitul.Text.Equals("Ing. arch.") _ 
    OrElse Not txtTitul.Text.Equals("MUDr.") _ 
    OrElse Not txtTitul.Text.Equals("MVDr.") _ 
    OrElse Not txtTitul.Text.Equals("RNDr.") _ 
    OrElse Not txtTitul.Text.Equals("PharmDr.") _ 
    OrElse Not txtTitul.Text.Equals("PhDr.") _ 
    OrElse Not txtTitul.Text.Equals("JUDr.") _ 
    OrElse Not txtTitul.Text.Equals("PaedDr.") _ 
    OrElse Not txtTitul.Text.Equals("ThDr.") Then 
    MsgBox("Neplatny titul!") 
    Exit Sub 
End If 

回答

6

你不想使用OrElseAndAlso,因爲只有當它不是其中的一個它是一個無效的標題(所以不是第一次沒有第二不是第3個等等....)。

但我可以告訴你一個更簡單和更易於維護的方法嗎?

Dim allowedTitles = {"Bc.","Ing.","Ing. arch.","MUDr.","MVDr.","RNDr.","PhDr.","PaedDr.","ThDr."} 
If Not allowedTitles.Contains(txtTitul.Text) Then 
    MsgBox("Invalid title!") 
End If 

如果你也想接受較低的情況下,所以忽略的情況下,你可以使用:

If Not allowedTitles.Contains(txtTitul.Text, StringComparer.InvariantCultureIgnoreCase) Then 
    MsgBox("Invalid title!") 
End If 
1

考慮你的輸入是「BC」。

Not txtTitul.Text.Equals("Bc.") <- false 
Not txtTitul.Text.Equals("")  <- true 
false OrElse true = true 

考慮你的輸入是 「ABC」

Not txtTitul.Text.Equals("Bc.") <- true 
Not txtTitul.Text.Equals("")  <- true 
true OrElse true = true 

對於解決方案,您可以考慮添的回答。

相關問題