473,386 Members | 1,758 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,386 software developers and data experts.

Setting default event handler

Suppose that there is a class I've created, named MyClass.
I defined an event MyEvent by
public event EventHandler MyEvent;

then I fire the event somewhere in the class like this
this.MyEvent(sender,args); <----(1)

It works fine as long as the event is consumed like this
MyClass aClass = new MyClass();
aClass.MyEvent+=new EventHandler(....); <---(2)

But if (2) is omitted, (because the caller doesn't need to consume
the event) then a NullReferenceExepction occurs at (1).

I think I can avoid this by adding an EventHandler in MyClass like
this.MyEvent+=new EventHanlder(...);
but is this the normal way? It looks like a waste because I also
have to create a method that doesn't do anything. Is there any more
efficient way to avoid this?
Thank you.

Aug 3 '06 #1
2 2409
If you copy the delegate instance into a local variable (to prevent
race conditions) and then check that local variable's equality with
null before firing the event, you'll avoid the situation where a
NullReferenceException is thrown.

EventHandler eh = MyEvent;
if (eh != null)
{
eh(sender, args);
}

Cheers,

Jono

Sin Jeong-hun wrote:
Suppose that there is a class I've created, named MyClass.
I defined an event MyEvent by
public event EventHandler MyEvent;

then I fire the event somewhere in the class like this
this.MyEvent(sender,args); <----(1)

It works fine as long as the event is consumed like this
MyClass aClass = new MyClass();
aClass.MyEvent+=new EventHandler(....); <---(2)

But if (2) is omitted, (because the caller doesn't need to consume
the event) then a NullReferenceExepction occurs at (1).

I think I can avoid this by adding an EventHandler in MyClass like
this.MyEvent+=new EventHanlder(...);
but is this the normal way? It looks like a waste because I also
have to create a method that doesn't do anything. Is there any more
efficient way to avoid this?
Thank you.
Aug 3 '06 #2
Thanks. Why I didn't come up with that simple if statement.
Thanks again!

Jonathan wrote:
If you copy the delegate instance into a local variable (to prevent
race conditions) and then check that local variable's equality with
null before firing the event, you'll avoid the situation where a
NullReferenceException is thrown.

EventHandler eh = MyEvent;
if (eh != null)
{
eh(sender, args);
}

Cheers,

Jono

Sin Jeong-hun wrote:
Suppose that there is a class I've created, named MyClass.
I defined an event MyEvent by
public event EventHandler MyEvent;

then I fire the event somewhere in the class like this
this.MyEvent(sender,args); <----(1)

It works fine as long as the event is consumed like this
MyClass aClass = new MyClass();
aClass.MyEvent+=new EventHandler(....); <---(2)

But if (2) is omitted, (because the caller doesn't need to consume
the event) then a NullReferenceExepction occurs at (1).

I think I can avoid this by adding an EventHandler in MyClass like
this.MyEvent+=new EventHanlder(...);
but is this the normal way? It looks like a waste because I also
have to create a method that doesn't do anything. Is there any more
efficient way to avoid this?
Thank you.
Aug 3 '06 #3

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

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
2
by: Hasan Ammar | last post by:
Is it possible to set up hotkeys using onkeypress? I know it can be done with the usual alphanumeric keys, but what about function keys? or using ctrl/alt combinations? Does anybody have a...
7
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
3
by: Xarky | last post by:
Hi, I have a page with 2 textboxes(Name and Surname) and two buttons(Submit and Reset). Now I have set the tab for them 1 Name 2 Surname 3 Submit 4 Reset
1
by: David Veeneman | last post by:
I am writing a control that relies on its host to validate the contents of one of its fields. The control fires a custom 'FooNeedsValidating' event and passes the field's data with the event. The...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
8
by: hanseymoon | last post by:
How do you create a text input box, which shows a default value of 1+ and blocks the user from deleting it? Thanks so much! :))
7
by: Academic | last post by:
What are the different effects of the following two statements: C1.Cursor = Cursors.WaitCursor C1.Cursor.Current = Cursors.WaitCursor I believe the first replaces the entire C1.Cursor...
6
by: John H Clark | last post by:
I am designing a site that requires AnonymousID. I set my web.config to allow this using <anonymousIdentification enable="true".../as recommended in the documentation. To verify the settings I...
4
by: David Veeneman | last post by:
I'm creating a UserControl that uses a LinkLabel. For reasons that I won't bore everyone with, I don't want the LinkLable to show the default hand cursor when the mouse enters the control. Should...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.