2

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"

4 Answers 4

18

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)
{
}
Sign up to request clarification or add additional context in comments.

Comments

4

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);
    }

Comments

3

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

Comments

-2

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
                                       };

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.