Here i have my deposit class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace CollectionAccounts
{
    public class DepositAccount : Account,IDisplayable
    {
        private double interestRate;
        public DepositAccount(string id)
            : base(id)
        {
            interestRate = 0;
        }

        public double InterestRate
        {
            get;private set;
        }
        public void AddInterest()
        {
            balance *= 1 + interestRate / 100;
        }

        public override void Withdraw(double amount)
        {
            if (amount <= (balance))
            {
                balance -= amount;


            }
            else
                Console.WriteLine("Not allowed as amount exeeds balance\n");
        }
       public void Display()
        {
            Console.WriteLine("The id is {0} and balance is {1}", id, balance);

        }
    }
}

...............................................................................................
The problem resides in the last line here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionAccounts
{
    class Program
    {
        static void Main(string[] args)
        {
        Account[] obj = new Account[2];

        obj[0] = new CurrentAccount("ca001");
        obj[1] = new DepositAccount("da001");


        obj[0].Lodge(200);
        ((CurrentAccount)obj[0]).Creditlimit = 500;
        obj[0].Withdraw(500);
        ((CurrentAccount)obj[0]).Display();


          obj[1].Lodge(200);
          ((DepositAccount)obj[1]).interestRate = 2.5;  ?????Does not work



        }
    }
}

I tried ((DepositAccount)obj[1]).AddInterest = 2.5;
Does not work too. Please help.

InterestRate has a private setter, so it's not accessible to callers. AddInterest is a method with no arguments, so the correct call would be:

((DepositAccount)obj[1]).AddInterest();

However, since your interest rate is never modified within the class, you might consider removing the public qualifier on the setter.

If i use the method as above,how do i set the interest to 2.5 ?

You change your class so that it's possible. As it stands, the interest can only be set from within the class.

ok,thanks have one more question here is my Current acc:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionAccounts
{
    public class CurrentAccount : Account,IDisplayable
    {
        private double creditlimit;
        public CurrentAccount(string id)
            : base(id)
        { creditlimit = 0; }

        public double Creditlimit
        {
            get { return creditlimit; }
            set { creditlimit = value; }
        }

        public  void Display()
        {
            Console.WriteLine("Id:  {0} balance:  {1}  creditlimit:  {2}", id, balance,creditlimit);

        }
        public override void Withdraw(double amount)
        {

            if (amount <= (balance + creditlimit))
            {
                balance -= amount;

            }
            else
                Console.WriteLine("Insufficient funds\n");


        }



    }
}

And here Deposit:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace CollectionAccounts
{
    public class DepositAccount : Account,IDisplayable
    {
private double interestRate;
        public DepositAccount(string id)
            : base(id)
        {
            interestRate = 3.5;
        }

        public double InterestRate
        {
            get;private set;
        }
        public void AddInterest()
        {
            balance *= 1 + interestRate / 100;
        }

        public override void Withdraw(double amount)
        {
            if (amount <= (balance))
            {
                balance -= amount;


            }
            else
                Console.WriteLine("Insufficient funds\n");
        }
       public void Display()
        {
            Console.WriteLine("Id: {0}  balance:{1}   ", id, balance);

        }
    }
}

Here main: and the problem is the program crashes after additing last 4 lines

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CollectionAccounts
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Simple array");
        Account[] obj = new Account[2];

        obj[0] = new CurrentAccount("ca001");
        obj[1] = new DepositAccount("da001");


        obj[0].Lodge(200);
        ((CurrentAccount)obj[0]).Creditlimit = 500;
        obj[0].Withdraw(500);
        ((CurrentAccount)obj[0]).Display();


          obj[1].Lodge(200);
          ((DepositAccount)obj[1]).AddInterest() ;
          obj[1].Withdraw(500);
          ((DepositAccount)obj[1]).Display();

          Console.WriteLine("ArrayList");
          ArrayList myList = new ArrayList();
          CurrentAccount acc = new CurrentAccount("c002");
          myList.Add(acc);
          myList.Add(new CurrentAccount("c002"));

          ((CurrentAccount)myList[0]).Lodge(400);
          ((CurrentAccount)myList[0]).Creditlimit =500;
          ((CurrentAccount)myList[0]).Withdraw(450);
          ((CurrentAccount)myList[0]).Display();

          ((DepositAccount)myList[1]).Lodge(400);
          ((DepositAccount)myList[1]).AddInterest();
          ((DepositAccount)myList[1]).Withdraw(500);
          ((DepositAccount)myList[1]).Display();



        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.