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
Right-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.
The defaultValue property sets or returns the default value of a text field. Note: The default value is the value specified in the HTML value attribute.
Users can create default values for an object property, which take precedence over the factory-defined values. Objects use default values when: Created in a hierarchy where an ancestor defines a default value. Parented into a hierarchy where an ancestor defines a default value.
The DefaultValue property specifies text or an expression that's automatically entered in a control or field when a new record is created. For example, if you set the DefaultValue property for a text box control to =Now(), the control displays the current date and time.
The Year
property of DateTime
is 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 Year
in that case. This is a good place to use a nullable
int? _selYear;
public int SelectedYear {
get { return _selYear ?? DateTime.Today.Year; }
set {
_selYear = value;
UpdateCalendar();
}
}
Use AmbientValueAttribute
to indicate that when you set the property to the default value, it actually means to get the current date.
int _selYear = 0;
[AmbientValue(0)]
[DefaultValue(0)]
public int SelectedYear{
get{
if (_selYear == 0)
return DateTime.Today.Year;
return _selYear;
}
set{
_selYear = value;
UpdateCalendar();
}
}
If you want to use the 0 value, then you can make it nullable, but never return null, only allow setting null:
int _selYear = null;
[AmbientValue(null)]
[DefaultValue(null)]
public int? SelectedYear{
get{
return _selYear ?? DateTime.Today.Year;
}
set{
_selYear = value;
UpdateCalendar();
}
}
Now, it is the null value that represents the current year.
The AmbientValueAttribute means: if you set this property to that value, when you get the property, another value will be returned from the environment, and that the return value in that case may vary, just like the current date.
You should initialize a value of a property explicitly. The DefaultValue attribute does not initialize property by the value it just provide metadata which value is default.
You can initialize it in ctor of a type or you can write a custom code which will reflect a type and initialize properties by the values from the type metadata.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With