I’ve created custom list:
public class BList : IEnumerable<Position>
{
private List<Position> positions = new List<BPosition>();
public void Add(Position p)
{ this.positions.Add(p); }
...
}
Than this list is member of the class Invoice:
public class Invoice : Record
{
public BList Positions = null;
public Invoice()
: base()
{
Init();
this.Positions = new BList(this);
}
...
}
Now in form constructor I populate collection of the Invoices and add positions to BList:
foreach(Invoice invoice in this.invoices)
{
// Load position for current invoice from the database
List<Position> positionsTmp = new List<Position>();
DB.LoadRecordset(positionsTmp, "id_invoice=3");
// and add to invoice positions items
positionsTmp.ToList().ForEach(p => invoice.Positions.Add(p));
}
List of the positions in each invoice is populated correctly.
But when I use list this.invoices in other functions in this same class each invoice has Position list empty (Count = 0). It looks like positions are added localy and when application leaves the constructor all previously added positions do BList are cleared.
How I can populate BList for each invoice to they was available in entire class ?
Thanks
If the name of your class is “Invoice”, then the constructor must be called “Invoice” as well.
In the constructor of BList (I assume) you are intializing a local list called “positionsTmp”. Instead add the positions directly to “Positions”.