Array search returns no results

I have the following code that an array should look for:

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(); } 

and the array has the following results:

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

What am I doing wrong??!?

+1
c # visual-studio
source share
2 answers

Perhaps the error is here:

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

If you have a WinForms control, try something like this:

 string userInput = this.passInput.Text; 

You can also check the value of userInput in the debugger to make sure that it contains the expected value.

+4
source share

You did not provide information about all of your variables, but I suspect that the string

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

- a problem. If this.passInput is a control, you will get the type name of the control, not what the user entered into the control.

If this is true, you can simplify your code like this:

 if (passwordList.Contains(this.passInput.Text)) { MessageBox.Show("FOUND"); foundResult = 1; } 
+1
source share

All Articles