Ross Coded Classes

Programming tips with no rose colored glasses.

Event Properties

Don’t forget about event properties.

Sometimes an interface requires an event that really is the same as an existing event.  This can be accomplished by forwarding the new to the existing event. 

There is nothing wrong with this version:

public event EventHandler ValueChanged;

protected override void OnCheckedValueChanged()
{
      base.OnCheckedValueChanged();

      EventHandler handler = ValueChanged;
      if handler != null)
      {
            handler(this, EventArgs.Empty);
      }
}

This does the same thing and I think this version is cleaner:

public event EventHandler ValueChanged
{
      add { CheckedChanged += value; }
      remove { CheckedChanged -= value; }
}

Leave a Reply

Your email address will not be published.

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