473,386 Members | 1,745 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.

Null event stupidity?

Why, oh why is it necessary to test an event for null before raising
it?

Why isn't that case handled automatically, instead of forcing
developers to write three lines of wasted boilerplate code every time
an event is raised:

if (SomethingChanged != null) // wasted code
{ // more waste
SomethingChanged(...)
} // more waste

instead of simply:
SomethingChanged(...)

Either the C# compiler or the CLR should handle the null case
automatically.
Nov 15 '05 #1
7 1621
"Kevin Cline" <kc******@hotmail.com> wrote in message news:ba**************************@posting.google.c om...

An event is just a delegate, so what you are suggesting is that delegates should be able to be called without being instantiated and
the error ignored. In many cases you would want the error raised.

--
Michael Culley
Nov 15 '05 #2
Kevin,
The VB.NET compiler does the check for you automatically, via the RaiseEvent
keyword.
Either the C# compiler or the CLR should handle the null case
automatically. I really do not think its the CLR's place to handle the null case on a
delegate! As that would be "masking errors".
instead of simply:
SomethingChanged(...) Remember that "SomethingChanged" is shorthand for
"SomethingChanged.Invoke()", if the CLR suddenly avoided Null Reference
Exceptions when calling a method on a Null reference, should it do it in all
cases or just in exceptional cases (no pun intended).

Hope this helps
Jay
"Kevin Cline" <kc******@hotmail.com> wrote in message
news:ba**************************@posting.google.c om... Why, oh why is it necessary to test an event for null before raising
it?

Why isn't that case handled automatically, instead of forcing
developers to write three lines of wasted boilerplate code every time
an event is raised:

if (SomethingChanged != null) // wasted code
{ // more waste
SomethingChanged(...)
} // more waste

instead of simply:
SomethingChanged(...)

Either the C# compiler or the CLR should handle the null case
automatically.

Nov 15 '05 #3
In article <OD**************@TK2MSFTNGP11.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
Either the C# compiler or the CLR should handle the null case
automatically.


I really do not think its the CLR's place to handle the null case on a
delegate! As that would be "masking errors".


How do you figure? If there are no listeners, it's not an error. It's just a
message with no recipients. This model forces the programmer to say "If
anyone's listening, then say this to everyone that's listening. Otherwise,
don't say anything". How is that different from saying "Say this to anyone
that's listening"? If a tree falls in the woods....
instead of simply:
SomethingChanged(...)


Remember that "SomethingChanged" is shorthand for
"SomethingChanged.Invoke()", if the CLR suddenly avoided Null Reference
Exceptions when calling a method on a Null reference, should it do it in all
cases or just in exceptional cases (no pun intended).


But it's okay to use += or -= on a Null reference? You're not making any
sense here...

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Nov 15 '05 #4

"Dave Benjamin" <ra***@lackingtalent.com> wrote in message
news:slrnbqljfn.b74.ra***@lackingtalent.com...
In article <OD**************@TK2MSFTNGP11.phx.gbl>, Jay B. Harlow [MVP - Outlook] wrote:
Either the C# compiler or the CLR should handle the null case
automatically.


I really do not think its the CLR's place to handle the null case on a
delegate! As that would be "masking errors".


How do you figure? If there are no listeners, it's not an error. It's just

a message with no recipients. This model forces the programmer to say "If
anyone's listening, then say this to everyone that's listening. Otherwise,
don't say anything". How is that different from saying "Say this to anyone
that's listening"? If a tree falls in the woods....
An event is a field like any other when called. If its null its null, not an
event with no listeners.
While I agree that its annoying, there are other more important issues, like
thread safety.
instead of simply:
SomethingChanged(...)
Remember that "SomethingChanged" is shorthand for
"SomethingChanged.Invoke()", if the CLR suddenly avoided Null Reference
Exceptions when calling a method on a Null reference, should it do it in all cases or just in exceptional cases (no pun intended).


But it's okay to use += or -= on a Null reference? You're not making any
sense here...


+= and -= eventually call Delegate.Combine() and Delegate.Remove(), which
can take null parameters. Remember, all operators are static, and a delegate
modification creates a new delegate instead of modifying the original one.
--
.:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

Nov 15 '05 #5
"Dave Benjamin" <ra***@lackingtalent.com> wrote in message news:slrnbqljfn.b74.ra***@lackingtalent.com...
How do you figure? If there are no listeners, it's not an error. It's just a
message with no recipients. This model forces the programmer to say "If
anyone's listening, then say this to everyone that's listening. Otherwise,
don't say anything". How is that different from saying "Say this to anyone
that's listening"? If a tree falls in the woods....
But that would mean creating an instance of each event delegate for each object that is created.
But it's okay to use += or -= on a Null reference? You're not making any
sense here...


Of course. += calls the static operator that takes two references as parameter and returns a reference, so is perfectly acceptable
to work on a null reference:

//not sure if this is syntatically correct but will be close
public static operator MyClass +=(MyClass Ref1, MyClass Ref2)
{
if (Ref1 == null) Ref1 = new MyClass();
.... do some sort of add here
return Ref1
}

--
Michael Culley
Nov 15 '05 #6
In article <#f**************@tk2msftngp13.phx.gbl>, Michael Culley wrote:
"Dave Benjamin" <ra***@lackingtalent.com> wrote in message news:slrnbqljfn.b74.ra***@lackingtalent.com...
How do you figure? If there are no listeners, it's not an error. It's just a
message with no recipients. This model forces the programmer to say "If
anyone's listening, then say this to everyone that's listening. Otherwise,
don't say anything". How is that different from saying "Say this to anyone
that's listening"? If a tree falls in the woods....


But that would mean creating an instance of each event delegate
for each object that is created.


Well, it's all how you look at things. I didn't understand the null behavior
very well, but I get it now (thanks to you and Daniel). But you could think
of an event as a specialized collection object that contains only delegates,
such that calling the collection as a function results in the delegates
getting called. Obviously, that's not what's really going on here, but it
would be an equally plausible solution to the multiple listener problem. In
that case, you would just add/remove delegates to these collections as
desired, and all events would be callable even if they were empty.

Anyway, thanks for the insight.
But it's okay to use += or -= on a Null reference? You're not making any
sense here...


Of course. += calls the static operator that takes two references as parameter and
returns a reference, so is perfectly acceptable to work on a null
reference... [snip]


Ahh, I hadn't realized that it worked that way. I guess that makes sense,
but it's definitely a surprise to me. For instance, the following cannot hold:

a.equals(b) <--> a == b

Because null.equals(b) would be invalid. (Not sure if that's valid
semantically in C# since I'm very new to the language).

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Nov 15 '05 #7
"Dave Benjamin" <ra***@lackingtalent.com> wrote in message news:slrnbqoh4t.sn1.ra***@lackingtalent.com...
Well, it's all how you look at things. I didn't understand the null behavior
very well, but I get it now (thanks to you and Daniel). But you could think
of an event as a specialized collection object that contains only delegates,
such that calling the collection as a function results in the delegates
getting called. Obviously, that's not what's really going on here, but it
would be an equally plausible solution to the multiple listener problem. In
that case, you would just add/remove delegates to these collections as
desired, and all events would be callable even if they were empty.
That would mean that each event would need to be created when the object was created. This would make instantiating the object much
slower.
a.equals(b) <--> a == b


The == operator is defined as

public static bool operator ==(SomeObject A, SomeObject B)

--
Michael Culley
Nov 15 '05 #8

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

Similar topics

1
by: Mark | last post by:
Hi, I'm writing user controls with custom events. I have a parent custom event that exposes some abstract methods and some custom events. I have also created some new user controls that...
3
by: Peter Vermilye | last post by:
How can I dynamically create buttons and wire them to an event handler? I have seen how I can add buttons to an event handler that are pre-created as part of the codebehind using AddHandler, but I...
9
by: JamesB | last post by:
I want progress on my ftp upload, so I am trying to add an event. All the FTP stuff is in its own class (FTPClient). In the class header I have: Public Event SendProgress(ByVal bytes As Integer)...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
7
by: rynato | last post by:
I have a lengthy 'markup.php' file which consists of functions which take as input the various attribute values of HTML tags and spit out the HTML tag. My problem is that the function for <inputis...
28
by: rahul | last post by:
#include <stdio.h> int main (void) { char *p = NULL; printf ("%c\n", *p); return 0; } This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
3
by: suganya | last post by:
Hi Some professionals already has developed the project using menu. In my company, they have given me task to clear the error in that. It is a script file named as "menubarAPI4.js" which is kept...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...

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.