-6

This is bank account class:

namespace BankAccount
{
    public abstract class BankAccount
    {
        protected static int numberOfAccounts = 100001;
        private double balance;
        private string owner;
        private string accountNumber;

        public BankAccount()
        {
            balance = 0;
            accountNumber = numberOfAccounts + "";
            numberOfAccounts++;
        }

        public BankAccount(string name, double amount)
        {
            owner = name;
            balance = amount;
            accountNumber = numberOfAccounts + "";
            numberOfAccounts++;
        }

        public BankAccount(BankAccount oldAccount, double amount)
        {
            owner = oldAccount.owner;
            balance = amount;
            accountNumber = oldAccount.accountNumber;
        }
}
}

This is Checking account class:

namespace BankAccount
{
    class CheckingAccount : BankAccount
    {
        int fee = 15;
        public CheckingAccount(string name, double amount)
        {
           base.BankAccount(name, amount);
        }
        public new bool Withdraw(double amount)
        {
            double totalAmount = amount + fee;
            return base.Withdraw(totalAmount);
        }
    }
}

For, base.BankAccount(name, amount); I am getting error,

'BankAccount' does not contain a definition for 'BankAccount.'

Bank account is the base class and checking account inherits the base class. and when I remove the base keyword it says:

Non Invokable member cannot be used as a method.

In the main, I created an object, I want to accept a value in Main - a string and a double and then send it to CheckingAccount class and CheckingAccount constructor should run and then send the values to BankAccount constructor and do the calculations.

How can I fix the error

'BankAccount' does not contain a definition for 'BankAccount'

. ? Thanks,

Maciej S.
  • 752
  • 10
  • 22
  • 3
    public CheckingAccount(string name, double amount) : base(name, amount) { } – Daniel Feb 27 '19 at 22:13
  • Yeah, I am trying to Create an object for CheckingAccount: CheckingAccount myCheckingAccount = new CheckingAccount("Benjamin Franklin", 1000); and enter values into CheckingAccount constructor and then the values should be sent to BankAccount("Benjamin Franklin", 1000); – Rohan Juneja Feb 27 '19 at 22:13
  • 1
    What exactly, do you think `base.BankAccount` does? – EJoshuaS - Stand with Ukraine Feb 27 '19 at 22:13
  • The entered values in Main should be sent into the CheckingAccount constructor and then to BankAccount Class Constructor. – Rohan Juneja Feb 27 '19 at 22:14
  • @Daniel You should add that as an answer, not a comment – Phil M Feb 27 '19 at 22:15
  • You need to read about how to call the base constructor. https://stackoverflow.com/questions/12051/calling-the-base-constructor-in-c-sharp – Chetan Feb 27 '19 at 22:15

2 Answers2

1

C# Is not like Java where you call super() between the curly braces, you Need to add ' : base(name,Amout)' to call the super class constructor. Like this :

public CheckingAccount(string name, double amount) : base(name,amount)
{

}
Chetan
  • 6,711
  • 3
  • 22
  • 32
RLoris
  • 526
  • 5
  • 14
  • 1
    This is not the correct answer and may add attract down votes. I would suggest to remove it before it gets down voted. – Chetan Feb 27 '19 at 22:20
  • You are right, I was Just trying to help, I didn't read the whole constructor, saw the base at the end, I have edited my answer, I don't care about downvotes, I'm just here to help and share my experience and ideas. – RLoris Feb 27 '19 at 22:30
  • I got you. And I believe everyone who is trying to solve the problem here has pretty much the same intention as you. Anyways right answer helps everyone. – Chetan Feb 28 '19 at 00:24
0

You can call it in this way.

public CheckingAccount(string name, double amount) : base (name, amount)
{

} 
Ivan Salo
  • 811
  • 1
  • 9
  • 25