简体   繁体   中英

Creating instance of a class with List as a parameter in C#

Hello I am trying to create a bank application with 3 classes-Bank, Account, Mainprogram. In my Main program i have an option for the user to Add a new bank account. In my bank class i have my attributes, constructor and add account method.

class Bank
{
    // Attributes

    private List<int> accounts { get; set; }
    private int number_of_accounts { get; set; }

    //Constructor
    public Bank(List<int> accounts, int number_of_accounts)
    {
        this.accounts= accounts;
        this.number_of_accounts = number_of_accounts;
    }
        public void AddNewAccount(int accountNumber)
    {
        accounts.Add(accountNumber);
        Console.WriteLine("Account with number " + accountNumber+ "has been added!");
    }

Inside my Main I have a menu where user can choose 1.Add account where i call my method.

public static bool MainMenu()
        {
            Bank myBank = new Bank(accounts, 0); <----- Error here,

            switch ()
            {
                case "1":
                    // Function
                    Console.WriteLine("Write the account number desired:");
                    int userInput= Convert.ToInt32(Console.ReadLine());
                    myBank.AddNewAccount(userInput);
                    return true;
                case "2":
                    // Function

Line 3 in my MainMenu it says "The name 'accounts' does not exist in the current context".

The problem is that "accounts" doesn't exist, you have not created any variable called accounts yet. To solve the issue do the following:

var accounts = new List<int>(); 
Bank myBank = new Bank(accounts, 0);

OR

Bank myBank = new Bank(new List<int>(), 0);

You can just use default constructor in your class, like this :

class Bank
{
    public Bank()
    {
        this.accounts = new List<int>();
        this.number_of_accounts = 0;
    }
    ... rest of code (from your original question)
}

You must pay attention to some things:

Usually private is used to fields, not for properties. You have full control of your private fields, so usually you don't define as a private properties.

private readonly List<int> accounts;
private int number_of_accounts;

Also, number_of_accounts is redundant. It's accounts.Count . So it's better use accounts.Count and avoid problems if you forget update number_of_accounts when you modify accounts.

number_of_accounts must be public (and a property instead of a field because is public) and only with get (without set because you can't change the size directly, you do that inserting or removing values).

public int number_of_accounts => accounts.Count;

You don't need a constructor with a list of accounts. Simply create the list. Later, with methods like AddNewAccount , you add elements.

public Bank()
{
    this.accounts= new List<int>();
}

You can have a constructor with an initial list of values if you want. But avoid use that list because outside, your code can modify the list and it's better that your class have all control about their fields.

public Bank(List<int> accounts)
{
    this.accounts= new List<int>();
    this.accounts.AddRange(accounts);
}

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