Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A C# Property Class

0.00/5 (No votes)
7 Feb 2010 3  
A generic class to encapsulate properties with notification events

Soon after starting work in C#, there were a couple of code patterns that arose that made me miss the good ol' C/C++ pre-processor. One of these is the basic pattern of a class property with change notification.

You've got a class and you want the state of its properties to be observable via events. How many times have you written the following bit of code in some form or the other? Automatic properties help when you don't need notification, INotifyPropertyChange abstracts that event a little. Still, a lot of boilerplate.

C#
int _age;

public event EventHandler AgeChanged;

protected virtual void OnAgeChanged()
{
  if(Changed != null)
    Changed(this, EventArgs.Empty);
}

public int Age
{
  get{ return _age; }
  set
  {
    if(value != age)
    {
      value = age;
      OnAgeChanged();
    }
  }
}   

Having written a fair amount of MFC in the years prior I really wanted to be able to do something like:

C#
DECLARE_PROPERTY(Age, int)

and have a fancy macro that would handle the boiler plate. Alas there is no macro pre-processor in C#-land.

The introduction of .NET generics opens up the opportunity to model a class property as a class itself, while retaining type safety and the same assignment semantics as intrinsic types. So this little class (and a derived class with a cancellable event) has become a handy part of my toolbox.

C#
public class Property<T>
{
  protected T _value = default(T);

  public Property()
  {
  }

  public Property(T value)
  {
      _value = value;
  }

  public event EventHandler Changed;

  public virtual T Value
  {
    get { return _value; }
    set
    {
        if ((value != null && !value.Equals(_value)) ||
            (_value != null && !_value.Equals(value)))
        {
            _value = value;
            OnChanged();
        }
    }
  }

  protected virtual void OnChanged()
  {
    if (Changed != null)
        Changed(this, EventArgs.Empty);
  }

  public static implicit operator Property(T value)
  {
    return new Property(value);
  }
}   

Using it is just a matter of declaring a public field on a class declaration.

C#
class Person
{
  public readonly Property<int> Age = new Property<int>();

  public readonly Property<string> Name = new Property<string>();
}  

Interacting with the properties looks just like an intrinsic (due to the implicit two way cast operators on the Property class).

C#
Person don = new Person();
don.Name.Value = "Don";
don.Age.Value = 41;

if (don.Age > 40)
  Console.WriteLine("Ooooooold");

with the difference that every property now comes with a built in Changed event:

C#
don.Age.Changed += new EventHandler(Age_Changed);

So anyway, I've found this to be a handy pattern and thought perhaps you might as well.

P.S.: The cancellable derived class looks like this:

C#
class CancellableProperty<T> : Property<T>
{
  public CancellableProperty(T value)
    : base(value)
  {
  }

  public event CancelEventHandler Changing;

  public override T Value
  {
    set
    {
        if ((value != null && !value.Equals(_value)) ||
            (_value != null && !_value.Equals(value)))
        {
            CancelEventArgs args = new CancelEventArgs(false);
            OnChanging(args);
            if (args.Cancel == false)
            {
                _value = value;
                OnChanged();
            }
        }
    }
  }

  protected virtual void OnChanging(CancelEventArgs args)
  {
    if (Changing != null)
        Changing(this, args);
  }
}  

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here