Class array is created in a procedure and I am not able to access the array in other method

Below is my code with comments. Successfully load a class array in a method but unable to access it in another method. I must be missing something easy. Please help!

Thanks,
Richard

----- Array -------------------------------------------------------
public class badline
{
public int OpCode {get;set;}
public string Operation{get;set;}
public string Department {get; set;}
public string  Type {get; set;}
public string trannum {get;set;}
public DateTime bddate {get;set;}
public string partnum {get;set;}
public string partdesc {get;set;}
public string jobnum {get;set;}
public string trantype {get;set;}
public string acct {get;set;}
public decimal actual {get;set;}
public string posted {get;set;}
}
----- Load array --------------------------------------------------
public void LoadBudgetActualData()
{
List < badline> badlines = new List< badline>();
badline dline = new badline();
dline.OpCode = line.OpCode;
dline.Operation = line.Operation;
dline.Department = line.Department;
dline.Type = line.Type;
dline.trannum = Convert.ToDecimal(ActRow["PartTranwipms_TranNum"]).ToString();
dline.bddate = Convert.ToDateTime(ActRow["PartTranwipms_TranDate"]);
dline.partnum = Convert.ToString(ActRow["PartTranwipms_PartNum"]);
dline.partdesc = Convert.ToString(ActRow["PartTranwipms_PartDescription"]);
dline.jobnum = Convert.ToString(ActRow["PartTranwipms_JobNum"]);
dline.trantype = Convert.ToString(ActRow["PartTranwipms_TranType"]);
dline.acct = Convert.ToString(ActRow["Calculated_acct"]);
dline.posted = Convert.ToBoolean(ActRow["PartTranwipms_PostedToGL"]).ToString();
dline.actual = Convert.ToDecimal(ActRow["Calculated_sumlbr"]) + Convert.ToDecimal(ActRow["Calculated_sumbur"]);
badlines.Add(dline);
----- The next line shows a count of 13230 ------------------------------
MessageBox.Show(badlines.Count.ToString());
}
----- Loop through array loaded in LoadBudetActualData in a different method --------------------------------------------------------------------
public void LoadBudgetActualDetailData()
{
List< badline> badlines = new List< badline>()
----- The next line shows a count of 0 --------------------------------------
MessageBox.Show(badlines.Count.ToString());
----- stop 1 is never displayed
foreach(badline line in badlines)
{
MessageBox.Show("stop 1");
}
}

The issue you’re encountering is related to the scope of the badlines list. In your LoadBudgetActualData() method, you’re creating and populating the badlines list, but this list is local to that method. This means it isn’t accessible from other methods, like LoadBudgetActualDetailData().

In OOP (Object-Oriented Programming), understanding the scope of variables and how they can be accessed is crucial. In your case, to access the badlines list across different methods, you might consider defining it at the class level instead of within a specific method. This way, it can be accessed by any method within the class.

Also, a quick note: what you’re using here is a List , which is a dynamic collection, not an array. Both are used to store collections of data, but they have some differences in how they are used and managed in C#.

Understanding scope is key in OOP, make sure you brush up on these concepts and understanding before you put code into production there are some non trivial issues that can arise from these concepts regarding performance and security you’d want to make sure and avoid.

1 Like