Cant print elements of arraylist or list

When i call ReadList function it dosent print the elements,what i am doing wrong?i tried even with normal list.What i want to do is if i create 2 accounts by CreateAccount function, i want to print them both through ReadList.

namespace Bank
{
    class Program
    {        
        static void Main()
        {
            int option;
            do
            {

                Menu1 menu1 = new Menu1();
                Account account = new Account();
                option = menu1.CallMenu1();
                if (option == 1)
                {
                    account.CreateAccount();
                }else if (option == 2) {
                    account.ReadList();
                    break;
                }
                else
                {
                    break;
                }
            } while (true);
        }

    }

    class Account
    {
        int AccountNumber { get; set; }
        string Name { get; set; }
        float Balance { get; set; }



        public void CreateAccount()
        {
            int AccountNumberInput;
            string NameInput;
            float BalanceInput;
            ArrayList list = new ArrayList();

            Console.WriteLine("put id");
            AccountNumberInput = int.Parse(Console.ReadLine());
            Console.WriteLine("type name");
            NameInput =Console.ReadLine();
            Console.WriteLine("type balance");
            BalanceInput = float.Parse(Console.ReadLine());
            list.Add(AccountNumberInput);
            list.Add(NameInput);
            list.Add(BalanceInput);
        }
        public void ReadList()
        {
            ArrayList list = new ArrayList();
            foreach (string c in list)
            {
                Console.WriteLine(c);
            }
        }
    }
}
Jon Skeet
people
quotationmark

Your CreateAccount method does this:

  • Creates a new list
  • Populates the list
  • Does nothing with the list

Your ReadList method does this:

  • Creates a new (empty) list
  • Prints the contents of the list

The list created in CreateAccount() is never communicated outside the method - how do you expect the ReadList() method to know about it?

The simplest change would be to make CreateAccount() return the list it creates, then change ReadList to accept an ArrayList as a parameter. You'd change your Main method to store the result of CreateAccount in a local variable, then pass it to ReadList when it calls that.

However, beyond that:

  • I would avoid using ArrayList in any new code. It was superceded many, many years ago by List<T>.
  • Your CreateAccount method should surely do what it says: create an account. It should probably be a static method returning an Account... then your ReadList method could just be a PrintDetails instance method without any parameters, because you would call it on the instance returned by CreateAccount.
  • You should consider what you want to do if you someone tries to print the account before creating it...

people

See more on this question at Stackoverflow