2015-10-24 21 views

回答

1

您有很多可能性。一種選擇是要創建的單詞列表,允許和使用List.Any extension method這樣的:

dim words as new List(of string) From { "house", "flat", "semi", "duplex" } 
dim word = housetype.Text 
Console.WriteLine(words.Any (function(w) w = word)) 

它的優點,它很容易擴展,如果新詞要來(或者如果列表是從讀一個配置文件,等等)。

爲了使代碼更具可讀性,功能可以打包成擴展方法,如if (texBoxControl.ContainsOnlyValidWords()) then ...

0

可能是一個更好的解決方案,然後SkorrloreGaming-製作的回答......

Public Function CheckForAllowedWords() As String 
     Dim ok_word As String 
     Dim AllowedWords As String() = "house flat semi duplex".Split(" ") 
     For Each item As String In AllowedWords 
      If housetype.Text = item Then 
       ok_word = item 
       Exit For 
      Else 
       Continue For 
      End If 
     Next 
     Return ok_word 
    End Function 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     CheckForAllowedWords() 
     If CheckForAllowedWords() <> Nothing Then 
      MsgBox("Accepted") 
     Else 
      MsgBox("Not Accepted") 
     End If 
    End Sub 
+0

.contains - 那麼你不需要在數組中尋找 – peterG

相關問題