Home > Software design >  How can I acces list items in c# [closed]
How can I acces list items in c# [closed]

Time:09-28

I am making a very basic banking app and created csv file that holds the details of customers. I then read this csv file and convert the data into objects and properties of the Account class and stored these objects in a list. So i am trying to access the individual properties of these objects through the lists and i am getting an error. Is there a better way to access and use the object properties?

class Account
   {
    public string CustomerName;
    public string PIN;
    public double Balance;

    public Account(string rowData)
    {
        string[] data = rowData.Split(',');

        //parse data into properties
        this.CustomerName = data[0];
        this.PIN = data[1];
        this.Balance = Convert.ToDouble(data[2]);
    }

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public void ReadAccounts()
    {
        //Readthe contents of the csv file as individual lines
        string[] csvLines = System.IO.File.ReadAllLines("Accounts.csv");

        //accounts
        var accounts = new List<Account>();

        //split each row into column data
        for (int i = 1; i < csvLines.Length; i  )
        {
            Account acc = new Account(csvLines[i]);
            accounts.Add(acc);
        }
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        ReadAccounts();
        for (int i = 0; i < accounts.Count; i  )
        {
            //code dependent on Account instance properties
        }
    }

CodePudding user response:

You have to declare accounts global to the class, not locally inside ReadAccounts function

CodePudding user response:

I suspect that you problem is because you declare var accounts = new List<Account>(); in ReadAccounts() which makes it local to that function and then attempt to access it in Form2_Load where it will be out of scope.

Declare accounts at a higher level e.g.:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public List<Account> Accounts {get;set;}

    public void ReadAccounts()
    {
        //Readthe contents of the csv file as individual lines
        string[] csvLines = System.IO.File.ReadAllLines("Accounts.csv");

        //accounts
        Accounts = new List<Account>();

        //split each row into column data
        for (int i = 1; i < csvLines.Length; i  )
        {
            Account acc = new Account(csvLines[i]);
            Accounts.Add(acc);
        }
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        ReadAccounts();
        for (int i = 0; i < Accounts.Count; i  )
        {
            //code dependent on Account instance properties
        }
    }

CodePudding user response:

The problem you're seeing is because you declare accounts as a variable only in the scope of ReadAccounts with this line:

var accounts = new List<Account>();

This means that the method Form2_Load() doesn't know about accounts.

You have two options. One is to make accounts a variable declared in your class Form2 like this:

public partial class Form2 : Form
{
     var accounts = new List<Account>();
     ...
}

Or you could have ReadAccounts return the result to your Form2_Load method:

public List<Account> ReadAccounts()
{
    //Readthe contents of the csv file as individual lines
    string[] csvLines = System.IO.File.ReadAllLines("Accounts.csv");

    //accounts
    var accounts = new List<Account>();

    //split each row into column data
    for (int i = 1; i < csvLines.Length; i  )
    {
        Account acc = new Account(csvLines[i]);
        accounts.Add(acc);
    }


     return accounts;
}

private void Form2_Load(object sender, EventArgs e)
{
     var accounts = ReadAccounts();
     ...
}
  • Related