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);
}
}
}
}
Your CreateAccount
method does this:
Your ReadList
method does this:
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:
ArrayList
in any new code. It was superceded many, many years ago by List<T>
.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
.See more on this question at Stackoverflow