【问题标题】:Checking wether textboxes are equal with list items [closed]检查文本框是否与列表项相等[关闭]
【发布时间】: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 &amp;&amp; txtPassword.Text == acount.Password)

标签: c#


【解决方案1】:

我认为您正在寻找的是这样的:

if (txtUsername.Text == acount.Name && txtPassword.Text == acount.Password)

因此,如果任何帐户的用户名和密码都正确,您最终会打开新表单。

但请注意,当前循环的编写方式,即使用户为列表中的一个用户输入了有效凭据,您仍会在每个循环上打开 MessageBox,但好的循环除外。

我建议使用其他人建议的.Any,或者使用布尔值来跟踪匹配状态:

bool isFound = false;

foreach (Account account in accounts)
{
    if (txtUsername.Text == account.Name && txtPassword.Text == account.Text)
    {
        isFound = true; // we found a match
        break;          // no need to keep searching, we can break
    }
}

if (isFound)
    openForm();
else
    MessageBox.Show("...");

【讨论】:

    【解决方案2】:

    你可以使用Any方法来代替for循环。
    您的代码中还有一个错误,如果有多个匹配项,它会打开许多​​表单(因为找到一个后 for 循环没有被破坏)。

    所以,这里是代码的快速解决方案:

    // NOTE: REMOVE THE FOR LOOP as it's not needed anymore.
    // Just use the code below instead of the loop.
    
    if (acounts.Any(account => txtUsername.Text == account.Name && account.txtPassword == account.Password)) {
        openForm();
    } else {
        MessageBox.Show("Invalid password or username.");
    }
    

    如果您还没有添加以下 using 语句,请不要忘记添加:

    using System.Collections.Generic;
    using System.Linq;
    

    【讨论】:

      【解决方案3】:

      为处理决策调用创建一个扩展。

        public static class Extensions
          {
              public static void ReactWith(this Acount acct, Action succes, Action failure)
              {
                  Action actionToPerform = acct != null ? succes : failure;
                  actionToPerform.Invoke();
              }
          }
      

      在主文件中调用扩展名如下。

          private void OpenForm()
          {
             // launch form code here.
          }
          private void ErrorMesage()
          {
              // Message box code here.
          }
      
             List<Acount> acounts = new List<Acount>();
      
              var validAcct = acounts.Where(a => a.Name == "abc").FirstOrDefault();
              validAcct.ReactWith(OpenForm, ErrorMesage);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 2016-09-13
        • 2012-01-10
        • 1970-01-01
        • 2021-12-16
        • 2021-09-04
        • 2019-11-04
        相关资源
        最近更新 更多