Mikkel Nielsen Mikkel Nielsen - 11 days ago 5
C# Question

Trying to parse String to Datetime in property (C#)

Im trying to parse a string a DateTime object. No problem there :-)
My problem comes, when i try to do it directly in a property set method.

I thought i could do this:

private DateTime _fromDate;
public DateTime FromDate
{
get { return _fromDate; }
set { _fromDate = DateTime.Parse(value); }
}


But I get this error:

"Argument 1: cannot convert from 'System.DateTime' to 'string'"

Does anyone know how to do this, if it can be done at all?

Answer

Simple Use

private DateTime _fromDate;
public DateTime FromDate
    {
        get { return _fromDate; }
        set { _fromDate = value; }
    }

value will be of DateTime as the property is of DateTime. When someone will assign this property a DateTime object it will go in value. In Set that value will be assigned to _fromDate