473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

rasing custom event

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!
Nov 15 '05 #1
2 4869

"MC D" <as***@earthtalk.com> wrote in message
news:ON****************@TK2MSFTNGP10.phx.gbl...
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
Nov 15 '05 #2
Thanks Chad. That did the trick! Didn't realize that was an issue.

-MC D
"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:mq**********************@twister.austin.rr.co m...

"MC D" <as***@earthtalk.com> wrote in message
news:ON****************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Amey | last post by:
Hi all I am creating a basic control to perform some tasks, and I want to declare some events to be raised so they can be handled from the form that the control is on. I can create my own Event...
7
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently...
6
by: Christian H | last post by:
Hi! I've created a custom control (myDrawControl) that deals with drawing. This control is then added to a form( myMainForm) Now, whenever something is dragged and dropped onto myDrawControl ,...
3
by: jlea | last post by:
I've created a custom control based on TreeView and it handles several events, such as Mousedown. I added this custom control to the toolbox in another project, dragged the custom control to the...
15
by: Tinus | last post by:
Hello all, I've created a custom control that draws a monthly schedule (using the Draw function in C#). Basically it draws 31 boxes and writes the day number in every box. This works...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
3
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the...
0
by: Toco | last post by:
Hello. I have a program which processes some files when the user clicks a button. My 1st class is based on a Form object. When the user clicks on a button, another class is instantiated, then...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.