I need an event to fire when I change a property. When I run this code I get a Object reference not set to an instance of an object. What am I doing wrong? What is the correct way to instantiate an event, and fire it when a property is set?
public member:
public event System.EventHandler ClassChanged;
property set:
ClassChanged(this, EventArgs.Empty);
You need to verify the event handler it not null first:
if (ClassChanged != null)
ClassChanged(this, EventArgs.Empty);
But in general, you may want to wrap this up into a helper method like so:
private void NotifyClassChanged() {
if (ClassChanged != null)
ClassChanged(this, EventArgs.Empty);
}
Or possibly implement INotifyPropertyChanged instead.
Try this:
System.EventHandler handler = this.ClassChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With