简体   繁体   中英

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. 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. 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. 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. 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. 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. 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.

check if account != null to ensure you do not get a null reference exception when trying to get property values.

Then, use account.CheckBalance to get the property value for that particular account.

I may not be fully understanding the question and cannot comment because I do not have a 50 reputation : (

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