rasing custom event

  • Thread starter Thread starter Chad Myers
  • Start date Start date
C

Chad Myers

MC D said:
I have an event that I am defining in one class, and I want to subscribe to
it in another. I have the delegate for the event in the top of my class:

public delegate void statsUpdatedHandler(object sender, EventArgs e);

I then have the event defined in the class as:

public static event statsUpdatedHandler statsUpdated;

I raise the event in a procedure in the same class. It looks like:

statsUpdated(this,EventArgs.Empty);

I subscribe to the event in my other class by saying:

etalkManager.stats.statsUpdated += new
etalkManager.statsUpdatedHandler(getLatestData);

When the event fires, I get an error saying "object reference not set to an
instance of an object.", and it refers to the line that raises the event in
the original class (statsUpdated(this,EventArgs.Empty);)

When calling events, you must make sure that it is not null
(no handlers attached).

You should always make a private or protected method names
something like:

protected void OnStatsUpdated()
{
if( statusUpdated != null )
{
statsUpdated(this, EventArgs.Empty);
}
}

-c
 
I have an event that I am defining in one class, and I want to subscribe to
it in another. I have the delegate for the event in the top of my class:

public delegate void statsUpdatedHandler(object sender, EventArgs e);

I then have the event defined in the class as:

public static event statsUpdatedHandler statsUpdated;

I raise the event in a procedure in the same class. It looks like:

statsUpdated(this,EventArgs.Empty);

I subscribe to the event in my other class by saying:

etalkManager.stats.statsUpdated += new
etalkManager.statsUpdatedHandler(getLatestData);

When the event fires, I get an error saying "object reference not set to an
instance of an object.", and it refers to the line that raises the event in
the original class (statsUpdated(this,EventArgs.Empty);)

Thanks for any help!
 
Back
Top