SmartDate error when using MaxDate or MaxValue as empty value

SmartDate error when using MaxDate or MaxValue as empty value

Old forum URL: forums.lhotka.net/forums/t/11742.aspx


AvanLeeuwen posted on Monday, December 10, 2012

Normal 0 false false false EN-US X-NONE X-NONE

An error was encountered in performing a simple unit test for the SmartDate constructor, SmartDate(EmptyValue emptyValue) with the emptyValue being MaxDate:

Test below:

// test for maxDateValue

SmartDate target = new SmartDate(SmartDate.EmptyValue.MaxDate);

DateTime expected = DateTime.MaxValue;

DateTime actual = target.Date;

Assert.AreEqual(expected, actual);

 

The test fails with expected having the value of DateTime.maxValue and actual having the value of DateTime.minValue. The origin of the problem is due to _initialized being set to false. Therefore when the Date property is used to get the value stored in the SmartDate variable it returns DateTime.minValue when the empty date was set to DateTime.maxValue.  The error comes from the code below: 

 

/// <summary>

/// Gets or sets the date value.

/// </summary>

public DateTime Date

{

 get

 {

 if (!_initialized)

{

 _date = DateTime.MinValue;

 _initialized = true;

}

 return _date;

}

 

set

{

 _date = value;

 _initialized = true;

}

}

Recommended code change is as follows:

public DateTime Date

        {

            get

            {

                if (!this._initialized)

                {

                    // corrected 12/7/2012

                    // The empty value is already set in the constructor

                    // therefore the following code is more than likely not

                    // needed.

                    if (this._emptyValue == SmartDate.EmptyValue.MinDate)

                    {

                        this._date = DateTime.MinValue;

                    }

                    else

                    {

                        this._date = DateTime.MaxValue;

                    }

                    // _date = DateTime.MinValue;

                    // _initialized = true;

                }

                return this._date;

            }

 

            set

            {

                this._date = value;

                this._initialized = true;

            }

        }

 

In addition, I am not sure that the _initialized flag should be set to true when the empty value is used.  Because once set, the SmartDate.Add and SmartDate.Subtract methods could be used to modify the empty value where it no longer would be equal to the MinValue or MaxValue.

RockfordLhotka replied on Friday, December 14, 2012

I'll add this to the bug list.

RockfordLhotka replied on Sunday, February 24, 2013

This test passes in current CSLA .NET code, so the bug must exist in some older version and it has been fixed since.

Copyright (c) Marimer LLC