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".
CodePudding user response:
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);
CodePudding user response:
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)
}
CodePudding user response:
Also, the Bank class shouldn't take the number of accounts as a parameter. You are already passing through a list of the accounts (or account ids). It would be better to use the list of accounts to get the number of accounts like so:
private int number_of_accounts
{
get
{
return accounts.Count;
}
}
CodePudding user response:
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);
}