Home > database >  Checking for uninitialized DateTime properties?
Checking for uninitialized DateTime properties?

Time:08-02

I have a class that has numerous properties, two of which are dates:

public class AchFile
{
    public DateTime FileDate { get; set; }
    public DateTime EffectiveDate { get; set; }
    // other properties

    public int Insert()
    {
        //Set file date
        if (FileDate == null)
        {
            FileDate = DateTime.Today;
        }

        //Set effective date
        if (EffectiveDate == null)
        {
            EffectiveDate = ServiceUtil.NextBusinessDay(InterfaceId, FileDate);
        }
            
        return //....
    }
}

When I make an instance of the class, I'm not defining EffectiveDate or FileDate. This causes an issue if I call on .Insert() since DateTime objects cannot be null and therefore, those if statements won't be accessed properly.

What would be the best way to update the if statements?

Would the following make sense?

// Default value for a DateTime object is MinValue
if (FileDate == FileDate.MinValue)
{
  FileDate = DateTime.Today;
}

if (EffectiveDate == EffectiveDate.MinValue)
{
  EffectiveDate = ServiceUtil.NextBusinessDay(InterfaceId, FileDate);
}

CodePudding user response:

You could change the DateTime to DateTime? then they can be null, which seems to be the most accurate representation of what you want (a date that can have no value).

Then you change:

ServiceUtil.NextBusinessDay(InterfaceId, FileDate);

to

ServiceUtil.NextBusinessDay(InterfaceId, FileDate.Value);

However, if you want to avoid null, then the default(DateTime) (or just default in later C# versions) is what you want.

CodePudding user response:

If DateTime.MinValue will never be a normal value for those properties, you could check them against that. This feels somewhat unpleasant though, as effectively it becomes a magic value.

Another alternative is to make the properties nullable:

public DateTime? FileDate { get; set; }
public DateTime? EffectiveDate { get; set; }

Then you can check them against null, which will be the default value.

Note that if you're really only representing dates, then if you're using .NET 6 I'd recommend using the DateOnly type to make that clear.

CodePudding user response:

You can initialize properties on creation and do not check at all:

public class AchFile
{
    public DateTime FileDate { get; set; }    
    public DateTime EffectiveDate { get; set; } 

    public AchFile() { 
      ...
      FileDate = DateTime.Today;
      EffectiveDate = ServiceUtil.NextBusinessDay(InterfaceId, FileDate);
      ...
    }

    // other properties

    public int Insert() {
      ...
            
      return //....
    }
}

If it's not a way out (say, you can create ArchFile today, but start using it tomorrow) and you have to keep DateTime, not DateTime? you can assign some kind of default value to the properties

public class AchFile
{
    // 1 Jan 1 as a default value 
    public DateTime FileDate { get; set; }      = DateTime.MinValue;
    public DateTime EffectiveDate { get; set; } = DateTime.MinValue;
    // other properties

    public int Insert()
    {
        //Set file date
        if (FileDate == DateTime.MinValue)
        {
            FileDate = DateTime.Today;
        }

        //Set effective date
        if (EffectiveDate == DateTime.MinValue)
        {
            EffectiveDate = ServiceUtil.NextBusinessDay(InterfaceId, FileDate);
        }
            
        return //....
    }
}
  • Related