Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Default Value to a property on c# [duplicate]

Tags:

c#

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
like image 321
jcvegan Avatar asked Oct 24 '12 17:10

jcvegan


People also ask

How do I set default value in property?

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.

Can a property have default value?

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.

What is default property value?

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.

What is the use of default value in the properties?

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.


3 Answers

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();
  }
}
like image 191
JaredPar Avatar answered Nov 03 '22 01:11

JaredPar


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.

like image 43
Miguel Angelo Avatar answered Nov 02 '22 23:11

Miguel Angelo


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.

like image 42
Viacheslav Smityukh Avatar answered Nov 02 '22 23:11

Viacheslav Smityukh