简体   繁体   English

作业在对象列表中找到匹配的对象并访问该对象的属性

[英]Homework find matching object in a list of objects and access properties of that object

I am trying to create a program that mimics an ATM. 我正在尝试创建一个模仿ATM的程序。 In my program, I need to check if the string that a user enters matches the Name property of any objects within a list of objects. 在我的程序中,我需要检查用户输入的字符串是否与对象列表中任何对象的Name属性匹配。 If it does not match, then the account is automatically added with some other default values. 如果不匹配,则会自动为该帐户添加一些其他默认值。 If it does match, then I need to set the variables that are accessed on another form to the properties of that account object. 如果确实匹配,那么我需要将在另一个表单上访问的变量设置为该帐户对象的属性。 Additionally, those properties will need to be updated from the other form, so that the object is kept current. 此外,这些属性将需要从其他形式更新,以便使对象保持最新状态。 I think that I can figure out how to update those properties, but I am having difficulty with trying to set the variables to the current account, more specifically, how to access the properties of the matching account. 我认为我可以弄清楚如何更新这些属性,但是在尝试将变量设置为当前帐户时遇到了困难,更具体地说,是如何访问匹配帐户的属性。 My class constructor is as follows: 我的类构造函数如下:

class Account
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }
    private int acctNum = 0;
    public int AcctNumber
    {
        get
        {
            return acctNum;
        }
        set
        {
            acctNum = value;
        }
    }
    //initialize the CheckBalance value to 100.00
    private decimal checkBalance = 100.00M;
    public decimal CheckBalance
    {
        get
        {
            return checkBalance;
        }
        set
        {
            checkBalance = value;
        }
    }
    public Account(string Name)
    {
        this.Name = Name;
    }
    private decimal saveBalance = 100.00M;
    public decimal SaveBalance
    {
        get
        {
            return saveBalance;
        }
        set
        {
            saveBalance = value;
        }
    }
}

This works out just fine, as the only constructor that I need is the Name property, while the other properties are automatically set to a base value. 这样做很好,因为我唯一需要的构造函数是Name属性,而其他属性会自动设置为基值。 The list and relevant code that I currently have are as follows: 我当前拥有的列表和相关代码如下:

    //variable that will be used to check textbox1.Text
    string stringToCheck;
    //array of class Account
    List<Account> accounts= new List<Account>();
    public MainMenu()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //set value to user's input
        stringToCheck = textBox1.Text;
        //set a var that only returns a value if the .Name already exists
        var matches = accounts.Where(p => p.Name == stringToCheck);
        //check through each element of the array
        if (!accounts.Any())
        {
            accounts.Add(new Account(stringToCheck));
        }
        else if (matches != null)


                //set variables in another form. not sure if these are working
                Variables1.selectedAccount = ;
                //is this calling the CheckBalance of the instance?
                Variables1.selectedCheckBalance = accounts[i].CheckBalance;
                //same thing?
                Variables1.selectedSaveBalance = accounts[i].SaveBalance;

                //switch to form
                AccountMenu acctMenu = new AccountMenu();
                this.Hide();
                acctMenu.Show();




        }

In the above code, the "else if (matches != null)" is more of a filler, since I am not sure what to use. 在上面的代码中,“ else if(matches!= null)”更像是填充符,因为我不确定该使用什么。 Of course, I also need to re-write the portion "if (!accounts.Any())" because once the list is populated with at least one object, this code will never occur again. 当然,我还需要重新编写“ if(!accounts.Any())”部分,因为一旦列表中至少填充了一个对象,此代码将不再发生。 So, really, I just need to know how to check for a matching account and how to access the properties of that account so that I can set the Variables1 properties to match. 因此,实际上,我只需要知道如何检查匹配的帐户以及如何访问该帐户的属性,就可以将Variables1属性设置为匹配。 Thanks for any help! 谢谢你的帮助!

If it works for your particular situation, var account = accounts.FirstOrDefault(p => p.Name == stringToCheck) will give you the first account in the collection that matches the expression or null if nothing exists. 如果它适合您的特定情况,则var account = accounts.FirstOrDefault(p => p.Name == stringToCheck)将为您提供集合中与该表达式匹配的第一个帐户,如果不存在则为null。

check if account != null to ensure you do not get a null reference exception when trying to get property values. 检查account != null是否确保尝试get属性值时不会出现null引用异常。

Then, use account.CheckBalance to get the property value for that particular account. 然后,使用account.CheckBalance获取该特定帐户的属性值。

I may not be fully understanding the question and cannot comment because I do not have a 50 reputation : ( 我可能没有完全理解这个问题,因此无法发表评论,因为我没有50的声誉:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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