Raise Event in .NET 3,5 style property?

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?
 
P

Peter Duniho

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top