C# 搜索数组未返回任何结果
我有以下代码用于搜索数组:C# 搜索数组未返回任何结果,c#,visual-studio,C#,Visual Studio,我有以下代码用于搜索数组: for (int i = 0; i < this.passwordList.Length; i++) { string userInput = Convert.ToString(this.passInput); if(userInput == passwordList[i]) { MessageBox.Show("FOUND");
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();
}
我做错了什么 错误可能在这里:
string userInput = Convert.ToString(this.passInput);
如果您有WinForms控件,请尝试以下操作:
string userInput = this.passInput.Text;
您可能还想在调试器中检查
userInput
的值,以确保它包含您期望的值。错误可能在这里:
string userInput = Convert.ToString(this.passInput);
如果您有WinForms控件,请尝试以下操作:
string userInput = this.passInput.Text;
您可能还想在调试器中检查
userInput
的值,以确保它包含您期望的值。您没有提供有关所有变量的信息,但我怀疑这行
string userInput = Convert.ToString(this.passInput);
这就是问题所在。如果this.passInput
是一个控件,您将获得控件类型的名称,而不是用户在控件中输入的名称
如果这是真的,您可以将代码简化为如下内容:
if (passwordList.Contains(this.passInput.Text)) {
MessageBox.Show("FOUND");
foundResult = 1;
}
你还没有提供所有变量的信息,但我怀疑这行
string userInput = Convert.ToString(this.passInput);
这就是问题所在。如果this.passInput
是一个控件,您将获得控件类型的名称,而不是用户在控件中输入的名称
如果这是真的,您可以将代码简化为如下内容:
if (passwordList.Contains(this.passInput.Text)) {
MessageBox.Show("FOUND");
foundResult = 1;
}
谢谢,没错!!!我这边的愚蠢错误。。。当Stackoverflow允许我时,我将接受您的问题您的ToString()将返回类似于
“System.Windows.Forms.TextBox,Text:actual input”的内容!!!我这边的愚蠢错误。。。当Stackoverflow允许我时,将接受您的问题您的ToString()将返回类似于“System.Windows.Forms.TextBox,Text:actual input”
什么是passInput
??通过转换为string方法,我猜您可能对数据类型有点混淆。尝试调试并单步执行代码,以查看变量设置的值。什么是passInput
??通过转换为string方法,我猜您可能对数据类型有点混淆。尝试调试和单步执行代码,以查看变量设置的确切值。