473,402 Members | 2,061 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,402 software developers and data experts.

raising events in C#

When I raise an event I have to first test to see if the event has
been created. For example:

class ClassX
{
public event System.EventsArgs MyEvent;

public void SomeMethod()
{
if (MyEvent != null) MyEvent();
}
}

If there a way of creating the event variable (MyEvent) so it's list
is empty and when I write:

public void SomeMethod()
{
MyEvent();
}

it won't cause an exception and it will raise the event for each
subscriber in the list (and if nobody has subscribed the list will be
empty)?

Thanks in advance,
Darren.
Nov 16 '05 #1
4 1765
I don't know of any straight forward means for that....but of course you
could abstract it by using a simple method, and then invoke the method
instead....this would bring down the if block to a single line of code.
"Darren" wrote:
When I raise an event I have to first test to see if the event has
been created. For example:

class ClassX
{
public event System.EventsArgs MyEvent;

public void SomeMethod()
{
if (MyEvent != null) MyEvent();
}
}

If there a way of creating the event variable (MyEvent) so it's list
is empty and when I write:

public void SomeMethod()
{
MyEvent();
}

it won't cause an exception and it will raise the event for each
subscriber in the list (and if nobody has subscribed the list will be
empty)?

Thanks in advance,
Darren.

Nov 16 '05 #2
Add an empty dummy event handler in the constructor.

Niki

"Darren" <wa********@hotmail.com> wrote in
news:cd**************************@posting.google.c om...
When I raise an event I have to first test to see if the event has
been created. For example:

class ClassX
{
public event System.EventsArgs MyEvent;

public void SomeMethod()
{
if (MyEvent != null) MyEvent();
}
}

If there a way of creating the event variable (MyEvent) so it's list
is empty and when I write:

public void SomeMethod()
{
MyEvent();
}

it won't cause an exception and it will raise the event for each
subscriber in the list (and if nobody has subscribed the list will be
empty)?

Thanks in advance,
Darren.

Nov 16 '05 #3
Darren,

There is no way to do this, unless, as previously suggested, you create
a dummy event handler. This, IMO, is wasteful.

One important thing to point out. The way that you are firing events is
wrong, as it is not thread-safe. The recommended way for firing an event is
to assign the delegate list to a local parameter, and then check that for
null. If it is not, then fire that. The thing is between the check for
null and the firing of the event, the invocation list could have changed,
and could introduce an error.

So, you want to do this:

// Assign to local variable.
EventHandler eventHandler = MyEvent;

// Check for null.
if (eventHandler != null)
// Fire.
eventHandler(this, EventArgs.Empty);

Also, your declaration of your event is wrong, it should be:

public event System.EventHandler MyEvent;

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

"Darren" <wa********@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
When I raise an event I have to first test to see if the event has
been created. For example:

class ClassX
{
public event System.EventsArgs MyEvent;

public void SomeMethod()
{
if (MyEvent != null) MyEvent();
}
}

If there a way of creating the event variable (MyEvent) so it's list
is empty and when I write:

public void SomeMethod()
{
MyEvent();
}

it won't cause an exception and it will raise the event for each
subscriber in the list (and if nobody has subscribed the list will be
empty)?

Thanks in advance,
Darren.

Nov 16 '05 #4
Thanks Nicholas. Your answer wasn't what I was expecting but just the
answer I was looking for.
Nov 16 '05 #5

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

Similar topics

4
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
0
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
0
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
7
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text...
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?
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
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,...
0
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
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...

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.