jordanalexlivecom
jordanalexlivecom

Reputation: 9

Creating a constructor that inherits from a base class and includes an instance variable

I am trying to create a constructor in a derived class that takes the initialBalance from the base class and an instance variable call interestRate. I am receiving an error code CS7036, There is no argument given that corresponds to the required formal parameter 'balance' of 'Account.Account(decimal)'.

namespace BankApp
{
    class SavingsAccount : Account
    {
        private decimal interestRate;
        private decimal interest;

        public SavingsAccount(decimal percentage, decimal balance) // error is thrown here
        {
            interestRate = percentage;
            IntialBalance = balance;
        }  

        public void CalculateInterest()
        {
           interest = IntialBalance * interestRate;
        }
    }
}
namespace BankApp
{
    class Account
    {
        private decimal accountBalance;

        //constructor 
        public Account(decimal balance)
        {
            IntialBalance = balance; // validate the initial balance in property
        }
        // ensures intial balance is >= 0
        public decimal IntialBalance
        {
            get { return accountBalance; }
            set
            {
                if (value >= 0)
                    accountBalance = value;
                else
                    throw new ArgumentOutOfRangeException("IntialBalance", value, "Intial balance must be >= 0");
            }
        }
        // Returns current balance
        public decimal Balance
        {
            get { return accountBalance; }
        }
        // deposits into account
        public decimal Credit()
        {
            Console.WriteLine("Enter the amount you would like to deposit: ");
            decimal deposit = Convert.ToInt32(Console.ReadLine());

            accountBalance += deposit;
            return accountBalance;
        }
        // withdrawl from account
        public decimal Debit()
        {
            Console.WriteLine("Enter the amount you would like to withdrawl: ");
            decimal withdrawl = Convert.ToInt32(Console.ReadLine());

            if (accountBalance >= withdrawl)
                accountBalance = accountBalance - withdrawl;
            else
                Console.WriteLine("Debit amount exceeded account balance");
            return accountBalance;
        }
    }
}

Upvotes: 0

Views: 275

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39152

This error is occurring because your Account class does not have a default constructor. When you create your instance of SavingsAccount, it first tries to create an instance of Account, but does't know what to provide for the "balance" parameter of the only constructor you provided.

You could add a default constructor to the Account class to make the error go away, but then you'd have to manually set the balance.

The easiest solution would be to call the base constructor that receives the balance like this:

public SavingsAccount(decimal percentage, decimal balance) : base(balance)
{
    interestRate = percentage;            
}

Notice the : base(balance) on the end of the first line!

Also notice that we no longer need this line IntialBalance = balance;, since the base constructor will assign that value when it is called.

Upvotes: 1

jordanalexlivecom
jordanalexlivecom

Reputation: 9

nevermind, this is what I came up with. I keep trying to add : base(decimal balance) instead of just : base (balance). If there is a better way to do this I would be interested to see. I am very new to programing. Thanks.

 public SavingsAccount(decimal balance, decimal percentage) : base( balance) 
        {
            interestRate = percentage;
            InitialBalance = balance;
        }

Upvotes: 0

Related Questions