473,503 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 4628
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1563
by: (Pete Cresswell) | last post by:
I've been perusing a "real-life" application and notice that for instance, in a button's Click() event, they don't write the processing code. Instead, they raise an event like AddNewRecord and...
1
2144
by: Diotima | last post by:
I'm raising an event from a C# component and trying to catch it in a VB.NET Windows application. The VB app recognizes my custom EventArgs object and catches the event. However, when I try to read...
1
1787
by: Stanley J Mroczek | last post by:
I am trying to load a dropdownlist in VB when the edit is clicked in a datagrid. How do i call Sub loaddd to load the dropdownlist <asp:TemplateColumn runat="server" HeaderText="Id Type Option"...
0
1819
by: Fabre Lambeau | last post by:
I've got a problem when adding a CONSTRAINT CHECK on a table by calling a function. It just seems not to work... Here is the table (simplified to only the relevant fields for this case): ...
7
1627
by: Kevin Cline | last post by:
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...
8
6039
by: news-server.san.rr.com | last post by:
I have a class that defines a *static* event: | public static event FubarEventHandler FubarAdded; This event is raised in public static methods, such as: | public static void...
9
5142
by: Alex Shirley | last post by:
Hi there I’m simply trying to check for a blank or empty value in a textbox on my webform. In this instance I do not want to use a requiredfieldvalidator, I want to use a customvalidator (as I...
3
30506
by: Woo Mun Foong | last post by:
I have a checkbox, when enable, allows me to proceed with what I like to do. However, I need to check a certain conditions before I allow the checked box to be checked, if condition is not fullfill...
6
1620
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
0
7202
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
7460
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
5578
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,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
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...

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.