Possible Duplicate:
Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute
Im creating a control like a calendar, and I want user to set de year they want to view.
But If they don’t, the property of SelectedYear must be the actual year.
I try setting this Default Value like this:
[DefaultValue(DateTime.Today.Year)]
public int SelectedYear{
get{
return _selYear;
}
set{
_selYear = value;
UpdateCalendar();
}
}
And I got the following error
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
The
Yearproperty ofDateTimeis a calculated value but you’re trying to embed it into an attribute which is an immutable value stored in the binary. These two concepts aren’t compatible.What you want to do here is have a marker to note that the value isn’t set yet and to return the current
Yearin that case. This is a good place to use a nullable