views:

104

answers:

2

I have a set of properties and need to provide a default values for them. Sure I can do the following in getter:

 public string MyProp        {
        get { 
              if(!string.IsNulOrEmpty(_myProp))
                  return _myProp;
              else
                  return "Default";
            }

But I`d like it to look like

 [DefaultValue("Default")]
 public string Processes
 {
    get { return _processes; }

Is there a good way to do it with attributes? I`ve spent some time to look for some attribute or a way to do this but found nothing.

+2  A: 

I'd do:

private string processes = "default";

public string Processes
{
    ...
}
Noon Silk
Yes, absolutely agree with you and that`s my solution, just it can`t be done when implementing autoproperties. And if we have an attribute it could be done in a very cool way, just marking the property with some attribute that has default value.
Yaroslav Yakovlev
@Yaroslav Yakovlev: You are correct, automatic properties don't allow you to interact with the backing field at all. This is one of the drawbacks to them.
Scott Dorman
It's hard with an attribute because you'd also need to imply the type.
Noon Silk
A: 

You can't do this with attributes unless you use some sort of post-processor like PostSharp. The DefaultValueAttribute is used to inform the PropertyGrid (or other property browser type controls) of what the default value of the property should be so they can indicate when it has been changed from that default and reset it back to that default.

What you should do is something like this:

 private const string _processDefault = "Default";
 private string _processDefault  = _processDefault Default;
 public string Process
 {
    get
    { 
       return _processDefault ;
    }
    set
    {
        if (String.IsNullOrEmpty(value))
        {
           value = _processDefault ;
        }

       _myProp = value;
    }
 }

If you don't want the value to be set outside of your control make the setter private.

Using this approach you could still use the attribute, but would need to write some reflection code to get it's value and you would need to initialize the private _myProp variable through a function call rather than inline like I've shown.

Scott Dorman
Thanks for your reply. But actually it`s not about to use attributes. It`s about reducing the amount of code written and dry principle. I don`t want to repeat in all my properties if it is empty - return default value, otherwise - return it`s value. I want it to be short and descriptive like marking with attribute that will hold the default value.
Yaroslav Yakovlev
The only way you'll be able to accomplish something like that is to use a post-processing tool like PostSharp. The thing to keep in mind is that *ideally* property getters should be "side-effect free" which this would violate. Property getters are executed in a lot of places (including the watch/quick watch windows when debugging) and having side-effects can lead to some strange experiences.
Scott Dorman