Reputation: 9
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);
}
}
}
}
Upvotes: 0
Views: 123
Reputation: 21
Because your class is wrong. You are initialising your list inside one of your methods. Once your method completes execution, everything inside it is unloaded from the memory.
For this to work, you have to add a field/property to the class itself that contains the data and can be used by all the methods of the class
class Account
{
int AccountNumber { get; set; }
string Name { get; set; }
float Balance { get; set; }
//added this line
private ArrayList list {get;set;}
public void CreateAccount()
{
int AccountNumberInput;
string NameInput;
float BalanceInput;
//notice this
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()
{
//removed ArrayList list = new ArrayList();
foreach (string c in list)
{
Console.WriteLine(c);
}
}
}
Upvotes: 0
Reputation: 1501073
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
.Upvotes: 2