views:

279

answers:

5

Hello,

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"

Thank you!

+17  A: 

This code:

public AccountStatement()
{
    new AccountStatement(new Period(new NullDate().DateTime,newNullDate().DateTime), 0);
}

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:

public AccountStatement() : this(new Period(new NullDate().DateTime, new NullDate().DateTime), 0)
{
}
Brad Wilson
+3  A: 

Your parameter-less constructor creates a new instance of itself, but doesn't assign it to anything.

C. Lawrence Wenham
+4  A: 

Use

public AccountStatement() : this(new Period(new NullDate().DateTime,newNullDate().DateTime), 0) { }

insetad of

public AccountStatement()
    {
        new AccountStatement(new Period(new NullDate().DateTime,newNullDate().DateTime), 0);
    }
TcKs
A: 

Stupid, still sleeping I guess! Thanks all for the fast answer!

pmlarocque
A: 

You are calling a parameter-less constructor so AddStatementCharge is never initialized. Use something like:

var accountStatement = new AccountStatement(period, accountId) {
                                          StartDate = new Date(2007, 1, 1),
                                          EndDate = new Date(2007, 1, 31),
                                          StartingBalance = 125.05m
                                       };
Borek
cannot my parameter less constructor was calling the other one, and I don't have the accountID so can't call it directly.Must use the :this like stated previously. or simply put a return in front of my new in the parameter-less ctor.Thanks!
pmlarocque