rasing custom event

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
 
M

MC D

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!
 

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

Similar Threads


Top