2011-11-26 94 views
1

我有一個想搜索一個數組下面的代碼:搜索陣列沒有返回任何結果

for (int i = 0; i < this.passwordList.Length; i++) 

     { 

      string userInput = Convert.ToString(this.passInput); 

      if(userInput == passwordList[i]) 
      { 

       MessageBox.Show("FOUND"); 
       foundResult = 1; 
       break; 

      } 
      //MessageBox.Show(); 


     } 

和陣列具有以下結果:

public string[] passwordList = {"123456", "145784" , "asasas"}; 

我在做什麼錯!?!?

+0

什麼是'passInput'?通過轉換爲字符串方法,我猜測你可能在數據類型上有點混淆。嘗試通過代碼調試和逐步查看變量設置的值。 – 2011-11-26 18:57:31

回答

4

這個錯誤可能是在這裏:

string userInput = Convert.ToString(this.passInput); 

如果你有一個WinForms控制,嘗試這樣的事情,而不是:

string userInput = this.passInput.Text; 

您可能還需要檢查在調試器的userInput值以確保它包含您所期望的價值。

+0

謝謝,它的權利!愚蠢的錯誤從我身邊...將接受你的問題,當Stackoverflow允許我 –

+0

你的ToString()將返回類似於「」System.Windows.Forms.TextBox,文本:實際輸入「 –

1

你沒有帶您所有的變量提供的信息,但我懷疑行

string userInput = Convert.ToString(this.passInput); 

的問題。如果this.passInput是一個控件,您將獲得控件類型的名稱,而不是用戶輸入控件的名稱。

如果這是真的,你可以簡化你的代碼是這樣的:

if (passwordList.Contains(this.passInput.Text)) { 
    MessageBox.Show("FOUND"); 
    foundResult = 1; 
} 
相關問題