简体   繁体   中英

Searching array is not returning any results

I have the following code that is suppose to search an array :

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!?!?

The mistake is probably here:

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

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

string userInput = this.passInput.Text;

You might also want to inspect the value of userInput in a debugger to make sure that it contains the value that you expect.

You havn't provided information about all your variables, but I suspect the the line

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

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

If that is true you can simplify your code into something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM