Tuesday, February 8, 2011

C# 3.0 - Object initializer

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!

  • 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)
    {
    }
    
  • Your parameter-less constructor creates a new instance of itself, but doesn't assign it to anything.

  • 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);
        }
    
    From TcKs
  • Stupid, still sleeping I guess! Thanks all for the fast answer!

    From pmlarocque
  • 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
                                           };
    
    pmlarocque : 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!
    From Borek

0 comments:

Post a Comment