Raise Event in .NET 3,5 style property?

  • Thread starter Thread starter Joe Cool
  • Start date Start date
J

Joe Cool

Let's say you are writing a class library that you plan to sell or
others will be maing use of it. So, in order to provide the users as
much flexible features as possible, you may want to raise an event
when a/any property is changed.

Easy enough to do, just raise the event in the property's setter, as
in:

public event EventHandler myEvent;

private int myProperty;

public int MyProperty
{
get { return myProperty; }
set {
myProperty = value;
OnmyEvent(EventArgs.Empty);
}
}

Of course, OnmyEvent is a protected method that actually raises the
event if it exists.

Now, what if I wanted to use the .NET 3.5 way of short-circuiting
property definitions a la:

public int MyProperty { get; set; }

Is there any way to use the short-circuiting method of defining a
property AND raise an event in either the getter or setter?
 
[...]
Now, what if I wanted to use the .NET 3.5 way of short-circuiting
property definitions a la:

public int MyProperty { get; set; }

Is there any way to use the short-circuiting method of defining a
property AND raise an event in either the getter or setter?

No, not really. The auto-generated properties are strictly get/set, with
no opportunity for customization. Fortunately, even the explicit syntax
for properties is not all that bad. :)

Pete
 
Back
Top