473,568 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When is an event null?

Suppose I have this:

class C {
public delegate void MyEventHandler( );
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceEx ception?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceEx ception, and why?

Nov 16 '06 #1
22 16898
dv*****@gmail.c om wrote:
Suppose I have this:

class C {
public delegate void MyEventHandler( );
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceEx ception?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceEx ception, and why?
You'll get a NullReferenceEx ception if no-one has subscribed to the
event.
By declaring a "field-like event" you get an event (which is basically
an add/remove pair) and a field of the delegate type. The field's value
is null if no-one has subscribed to it.

See http://www.pobox.com/~skeet/csharp/events.html for a rather fuller
description.

Jon

Nov 16 '06 #2
You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.

Regards,

Bela Istok
<dv*****@gmail. comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
Suppose I have this:

class C {
public delegate void MyEventHandler( );
public event MyEventHandler MyEvent;

public void foo() {
MyEvent(); // NullReferenceEx ception?
}
}

Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceEx ception, and why?

Nov 16 '06 #3
Suppose I have this:
>
class C {
public delegate void MyEventHandler( );
public event MyEventHandler MyEvent;
public void foo() {
MyEvent(); // NullReferenceEx ception?
}
}
Under what circumstances will trying to raise the event in the foo
method generate a NullReferenceEx ception, and why?
The event will be null until an event handler is actually added to it. And,
it will be null after the last event handler is removed from it. The reason
is that the code that you posted really compiles to something that looks
a little more like this:

class C {
{
public delegate void MyEventHandler( );
private MyEventHandler _MyEvent;
public void add_MyEvent(MyE ventHandler value)
{
_MyEvent = (MyEventHandler )Delegate.Combi ne(_MyEvent, value);
}
public void remove_MyEvent( MyEventHandler value)
{
_MyEvent = (MyEventHandler )Delegate.Remov e(_MyEvent, value);
}
public void foo() {
MyEvent();
}
}

An event compiles to a private field that holds your delegate instance and
two methods that add and remove handlers to and from the delegate. When the
class is created, the field is null. When a handler is added to the event,
Delegate.Combin e() is called and that creates your delegate or adds the handler
to your delegate if it is already created. When a handler is removed from
the event, Delegate.Remove () is called and that removes the handler from
your delegate sets it to null if there aren't anymore handlers.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 16 '06 #4
Bela Istok wrote:
You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.
You certainly need to do the check, but in a multi-threaded environment
that's not enough.
See http://www.pobox.com/~skeet/csharp/lockchoice.shtml for more on
this.

(In environments where only single-threaded subscribe/unsubscribe/raise
are supported, the above is okay.)

Jon

Nov 16 '06 #5
JR
It probably should have been

http://www.yoda.arachsys.com/csharp/...ckchoice.shtml

JR

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Bela Istok wrote:
>You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.

You certainly need to do the check, but in a multi-threaded environment
that's not enough.
See http://www.pobox.com/~skeet/csharp/lockchoice.shtml for more on
this.

(In environments where only single-threaded subscribe/unsubscribe/raise
are supported, the above is okay.)

Jon

Nov 16 '06 #6
Skeet, your link 404s. I can see why this would be confusing though -
the event has collection-like semantics, and one imagines that calling
the event would involve simply iterating across the list and calling
the handlers... so one would expect the empty event-handler-list would
simply be handled by iterating across an empty list - not by returning
a null complaint. I guess one could just wrap it in a generic
"CallEvent" function or something that does the null-check for you.

Jon Skeet [C# MVP] wrote:
Bela Istok wrote:
You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.

You certainly need to do the check, but in a multi-threaded environment
that's not enough.
See http://www.pobox.com/~skeet/csharp/lockchoice.shtml for more on
this.

(In environments where only single-threaded subscribe/unsubscribe/raise
are supported, the above is okay.)

Jon
Nov 16 '06 #7
Jon,

First thanks for the link.

Second a question about you suggested method of implementing an event.

If "handler = someEvent", is not "handler" a reference to "someEvent" so
when "someEvent" loses the event handler would not "handler" also lose the
event handler?

Regards,
John

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** **************@ h54g2000cwb.goo glegroups.com.. .
Bela Istok wrote:
>You always need to check
If(MyEvent != null)

Because an event is null when no one is subscribed.

You certainly need to do the check, but in a multi-threaded environment
that's not enough.
See http://www.pobox.com/~skeet/csharp/lockchoice.shtml for more on
this.

(In environments where only single-threaded subscribe/unsubscribe/raise
are supported, the above is okay.)

Jon

Nov 16 '06 #8
Martin Z <ma***********@ gmail.comwrote:
Skeet, your link 404s.
Yup, sorry about that - as JR pointed out, the link should be

http://www.yoda.arachsys.com/csharp/...ckchoice.shtml
I can see why this would be confusing though -
the event has collection-like semantics, and one imagines that calling
the event would involve simply iterating across the list and calling
the handlers... so one would expect the empty event-handler-list would
simply be handled by iterating across an empty list - not by returning
a null complaint. I guess one could just wrap it in a generic
"CallEvent" function or something that does the null-check for you.
Well, it's consistent with a collection being null as well. I agree it
would be nice if delegates had an easy way of creating an "empty"
handler list, and indeed you can start off an event that way and avoid
the null check, but the default value of a field being null *is*
consistent with other semantics.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 16 '06 #9
John J. Hughes II <no@invalid.com wrote:
First thanks for the link.

Second a question about you suggested method of implementing an event.

If "handler = someEvent", is not "handler" a reference to "someEvent" so
when "someEvent" loses the event handler would not "handler" also lose the
event handler?
No, "handler" and "someEvent" are both variables. After the assignment,
if "someEvent" changes to refer to a different delegate (or null) that
won't change the value of "handler".

Note that delegates are immutable - using += and -= doesn't change the
list of handlers within a specific delegate, it returns a *new*
delegate with a different list.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 16 '06 #10

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

Similar topics

3
1750
by: Bruce Wood | last post by:
I need to remove all handlers from a particular event. I am doing this in the class where the event is defined. I have this vague memory that I can say: this.MyEvent = null; and this will clear all delegates from the event, but I'm not sure, and I can't find the relevant documentation.
1
1650
by: Amit Dedhia | last post by:
Hi I am developing an application using C++/CLI in Visual Studio 2005. In my application I have following structure. (1) Class A creates instances of Class B and Class C. (2) Class B implements a timer function which repeatedly executes a timer every 250 ms. (3) Class C instance hooks for a event raised by Class B instance. Class B...
0
1437
by: Matt Michler | last post by:
I have a WebUserControl with a custom event, when this event is fired, the delegate or event object is throwing a null reference exception. The odd part of this is, it used to work flawlessly, now it never works. I'm not sure what change affected the event while my coworker and I were working on this control. Here's the event code in the...
6
1294
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to properly implement that, is there a better way than using state management variables for each timer as i'm doing in the outline below? private...
0
1068
by: kirk | last post by:
I have three events that I have created and manage with timers. Two of the timer event handlers, the last two in the code below, need to reset state management data, if the first event handler fires. My question, is what I have implemented the right way to design state management, or is there maybe a best practice to design this...
1
1218
by: anil kasar | last post by:
when event is occur then i want to show messagebox display on client side ?
0
7692
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7913
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6274
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5496
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2099
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.