I’m having a little problem and I don’t see why, it’s easy to go around it, but still I want to understand.
I have the following class :
public class AccountStatement : IAccountStatement { public IList<IAccountStatementCharge> StatementCharges { get; set; } public AccountStatement() { new AccountStatement(new Period(new NullDate().DateTime,newNullDate().DateTime), 0); } public AccountStatement(IPeriod period, int accountID) { StatementCharges = new List<IAccountStatementCharge>(); StartDate = new Date(period.PeriodStartDate); EndDate = new Date(period.PeriodEndDate); AccountID = accountID; } public void AddStatementCharge(IAccountStatementCharge charge) { StatementCharges.Add(charge); }
}
(note startdate,enddate,accountID are automatic property to…)
If I use it this way :
var accountStatement = new AccountStatement{ StartDate = new Date(2007, 1, 1), EndDate = new Date(2007, 1, 31), StartingBalance = 125.05m };
When I try to use the method ‘AddStatementCharge: I end up with a ‘null’ StatementCharges list… In step-by-step I clearly see that my list get a value, but as soon as I quit de instantiation line, my list become ‘null’
This code:
is undoubtedly not what you wanted. That makes a second instance of AccountStatement and does nothing with it.
I think what you meant was this instead: