【发布时间】:2021-05-18 12:29:58
【问题描述】:
我决定列出一个包含帐户的列表。使用 foreach 我可以自动检查所有帐户,而不是很多 elseif。在 foreach 正下方,我想检查文本框中的内容是否与帐户相同,如果是这种情况,则会打开一个新表单。
Acount a = new Acount("Admin", "123");
Acount b = new Acount("Admin2", "321");
List<Acount> acounts = new List<Acount>();
acounts.Add(a);
foreach(Acount acount in acounts)
{
if (txtUsername.Text == ???)
{
openForm();
}
else
{
MessageBox.Show("Invalid password or username.");
}
}
这里是Acount 类:
class Acount
{
public string Name{ get; set; }
public string Password{ get; set; }
public Acount()
{
}
public Acount(string name, string password)
{
this.Name= name;
this.Password= password;
}
}
【问题讨论】:
-
你能告诉我们你的
Account类吗? -
这取决于,您是在寻找引用相等还是特定属性等?
-
我添加了我的整个班级@PrasadTelkikar
-
您提到了文本框,但它们不在您的代码中,也许这就是缺少的?
-
if(txtUsername.Text == acount.Name && txtPassword.Text == acount.Password)
标签: c#