The dream of "half-automatic" Automation Properties

Everyone who has worked with WPF knows the interface INotifyPropertyChanged. It only defines the PropertyChanged-event, that should be called when a property’s value has been changed. The PropertyChanged-event is used by WPF’s Data Binding.

Normally a class fires the event in the set-Accessors of its properties. And that’s the problem why you can’t use Automation Properties for classes that implement INotifyPropertyChanged.

Let’s take a very simple example, a Person-class, that only contains a Name-Property:

public class Person
{
  public string Name { get; set; }
}

If you now want to implement INotifyPropertyChanged, you have to use a property backed explicitly with a private field:

public class Person:INotifyPropertyChanged
{
  private string _name;
  public string Name
  {
    get { return _name; }
    set
    {
      _name = value;
      if (PropertyChanged != null)
        PropertyChanged(this,
          new PropertyChangedEventArgs("Name"));
    }
  }
  public event PropertyChangedEventHandler PropertyChanged;
}

Wouldn’t it be nice to use a “half-automatic”-Property in the code-snippet above? The private-Field _name and the get-Accessor could be created. A keyword would be necessary to access the private-Field in the set-Accessor. I think the keyword could be autoField or something like that. If Microsoft would implement “half automatic”-Properties in C# 3.5 or 4.0, you could create the Person-class as below. But keep in mind that this is just a dream, I don’t know if something like this is planned, so the Person-class implemented like below wouldn’t work (today):

public class Person:INotifyPropertyChanged
{
  public string Name {
    get;
    set
    {
      autoField = value;
      if (PropertyChanged != null)
        PropertyChanged(this,
          new PropertyChangedEventArgs("Name"));
    }
  }
  
  public event PropertyChangedEventHandler PropertyChanged;
}

What do you think of “half-automatic”-Properties? Would it be a useful feature for you?

Share this post

Comments (3)

  • Frank Quednau Reply

    Hi Thomas,
    after LINQ and all that, I am currently not inclined at all for any new keywords. I think what we got now is already close to too much. If we look at ruby we have a language that allows a developer write programs in a way it almost looks as if we are extending the language. Ideally, C# would reach such a state without having to add new keywords. In this case I also think that for saving one line of code, a language extension goes too far, even if that minuscule.
    My 5 cents :)

    May 8, 2008 at 10:54 pm
  • fuulaluuf Reply

    It would be better to be able to provide a hint for the compiler to automatically implement the event when it creates the auto field:

    public class Person:INotifyPropertyChanged
    {

    [AutoPropertyType(NotifyPropertyChanged)]
    public string Name
    {
    get;
    set;
    }
    }

    April 9, 2009 at 10:02 pm
  • Roman S. Golubin Reply

    Hi Thomas,
    why autoField? My be extensible [PropertyChangedAttribute] ??

    public MyNotifyPropertyChangedAttribute : PropertyChangedAttribute where T : INotifyPropertyChanged
    {
    override protected void OnPropertyChanged(T sender, string PropertyName)
    {
    if (sender.PropertyChanged != null)
    sender.PropertyChanged(sender,
    new PropertyChangedEventArgs(PropertyName));
    }
    }

    [MyNotifyPropertyChangedAttribute]
    public object MyProperty {get;private set;}

    May 21, 2009 at 4:43 pm

Leave a Reply to fuulaluuf Cancel reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.