0

I have a model in which I can not set [DefaultValue] for DateTime, the object is used to generate a list of users from the powershella script. When I do not have default value and powershell return null this causes an error. I would like to make the date set to 01.01.1990 00:00:00 in the case of null

public class user
        {
            [DefaultValue("")]
            public string Name { get; set; }

            //[DefaultValue( )]
            public DateTime PasswordLastSet { get; set; }
        }
MatDob
  • 3
  • 2
  • Isn't it better to make DateTime nullable? Before you set the password for the first time, initial value might be null. public DateTime? PasswordLastSet { get; set; } – dropoutcoder Jun 26 '18 at 08:57
  • 1
    How about `[System.ComponentModel.DefaultValue(typeof(DateTime), "1900-01-01")]` – Matthew Watson Jun 26 '18 at 08:58

1 Answers1

-1

Why don't you use a constructor?

public class user
{
    public string Name { get; set; }

    public DateTime PasswordLastSet { get; set; }

    public user()
    {
        PasswordLastSet = DateTime.MinValue;
    }
}
omriman12
  • 1,644
  • 7
  • 25
  • 48