By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

C# Noob - Why check for null when raising event?

P: n/a
Hi there,

Im stepping into C# from VB.net. In all the examples ive seen about raising
events the following construct is used:

if (myevent != null)
myevent(this,args);

Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?

TIA

Richard
Mar 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Richard,

The underlying delegate will be null if no event handlers are assigned
to it, hence the need for the check.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Richard Coltrane" <rc@spamsux.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi there,

Im stepping into C# from VB.net. In all the examples ive seen about
raising events the following construct is used:

if (myevent != null)
myevent(this,args);

Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?

TIA

Richard

Mar 19 '07 #2

P: n/a
Richard Coltrane wrote:
Hi there,

Im stepping into C# from VB.net. In all the examples ive seen about raising
events the following construct is used:

if (myevent != null)
myevent(this,args);

Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?

TIA

Richard
Hi Richard,

When you define an event:

public event EventHandler MyEvent;

it doesn't have any handlers associated with it so MyEvent == null;
until you register an event handler(s) with the event:

SomeObj.MyEvent += new EventHandler(MyHandler);

That's why you need to check for null every time you want to fire the event.

Let me know if you need any further information.

Hope it helped,
Andrey
Mar 19 '07 #3

P: n/a
If you're wondering why it's not required in VB, it's because VB
automatically does the null check when you use RaiseEvent.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
"Richard Coltrane" wrote:
Hi there,

Im stepping into C# from VB.net. In all the examples ive seen about raising
events the following construct is used:

if (myevent != null)
myevent(this,args);

Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?

TIA

Richard
Mar 19 '07 #4

P: n/a
Thanks all.

Ive been using C# for about 3 days and im already beginning to appreciate
just how much cotton wool VB.Net wraps us in. :)
That's not neccessarily a bad thing but it is very noticable.

Once i can get out of the habit of "As"ing my method params and "Then"ing my
"If"'s, I think im really going to like this C# business.

Im also quite pleased about how easy it is to transfer concepts across the
platform. Syntax is different but the BCL sure is a great common
denominator.

Still not convinced about those blimmin' braces { } but i guess I shouldn't
expect a totally painless transition to the "dark side".

;)

Thanks
Richard
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:65**********************************@microsof t.com...
If you're wondering why it's not required in VB, it's because VB
automatically does the null check when you use RaiseEvent.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
"Richard Coltrane" wrote:
>Hi there,

Im stepping into C# from VB.net. In all the examples ive seen about
raising
events the following construct is used:

if (myevent != null)
myevent(this,args);

Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?

TIA

Richard

Mar 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.