How do you raise an event in C#?
An stock data download class has a BarList to which it either adds new bars or updates and replaces the last bar when the last bar changes realtime. Whenever this download class adds a new bar to the BarList class or changes its last bar, it also calls its NotifyOnBarsAdded or NotifyOnBarChanges. I'm trying to get the notify methods to raise events so that the Canvas class which handles these events can redraw the last bar or entire chart depending on which notify method is called. The problem is that when the NotifyOnBarsAdded class is called i get a NullReferenceException as try to raise the event. I am raising the event like this: NotifyBarAdded(this, EventArgs.Empty)
. Is this incorrect? Here's the code:
public class BarList : List< Bar >
{
private int historyHandle;
public event EventHandler NotifyBarChanged;
public event EventHandler NotifyBarAdded;
开发者_如何学Python public BarList(int historyHandle)
{
this.historyHandle = historyHandle;
}
public BarList()
{
// TODO: Complete member initialization
}
public void NotifyOnBarChange()
{
NotifyBarChanged(this,EventArgs.Empty);
}
public void NotifyOnBarsAdded()
{
NotifyBarAdded(this, EventArgs.Empty);
}
public long handle { get; set; }
}
What you're doing is correct, except you have to take into account that there could be no event handlers attached, which will give you a NullReferenceException. You either have to insert a null guard before calling or initialize the events with an empty delegate like this.
Checking for null:
var local = NotifyBarAdded;
if(local != null) local(this, EventArgs.Empty);
Initializing with an empty delegate will allow you to keep the calls you already have (i.e. no need to check for null).
public event EventHandler NotifyBarAdded = delegate {};
You always need to check to see if someone has actually hooked onto an event
public void NotifyOnBarChange()
{
if (NotifyBarChanged != null)
NotifyBarChanged(this,EventArgs.Empty);
}
They will take your object and call something like:
yourObject.NotifyBarChanged += ...their event handler ...;
精彩评论