473,396 Members | 1,996 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,396 software developers and data experts.

Naming convention for delegates, events and event handlers?

I'm trying to pin down a good naming convention for the 3 things
required to implement an event.

You need:
1) a delegate
2) an event
3) an event handler

Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong.

Cheers

Boz

delegate void DataAvailableEventHandler(string data);

class ServerClass
{
public event DataAvailableEventHandler DataAvailable;

public FireEvent()
{
if (DataAvailable != null)
{
DataAvailable("bang");
}
}
}

class ClientClass
{
public ClientClass(ServerClass server)
{
server.DataAvailable += new
DataAvailableEventHandler(ServerClass_DataAvailabl e);
}

private ServerClass_DataAvailable(string data)
{
Trace.WriteLine("I got some data:\n" + data);
}
}

Jan 16 '06 #1
10 17731
Boz,

#3 isn't so important, because that is a private implementation, and the
naming conventions don't deal with private members (after all, they are
private, and not exposed).

You have the naming conventions right for 1 and 2. The only thing I
would say is that your event handler should follow the pattern for events
already established.

This means that you have two parameters. The first is of type object,
named sender, which is the instance of the object that fired the event.

The second is a class that derives from EventArgs (or EventArgs itself,
if there is no extra information).

In this case, you would have a class named DataAvailableEventArgs which
exposes a property named Data, which would expose your string.

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

<jj********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I'm trying to pin down a good naming convention for the 3 things
required to implement an event.

You need:
1) a delegate
2) an event
3) an event handler

Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong.

Cheers

Boz

delegate void DataAvailableEventHandler(string data);

class ServerClass
{
public event DataAvailableEventHandler DataAvailable;

public FireEvent()
{
if (DataAvailable != null)
{
DataAvailable("bang");
}
}
}

class ClientClass
{
public ClientClass(ServerClass server)
{
server.DataAvailable += new
DataAvailableEventHandler(ServerClass_DataAvailabl e);
}

private ServerClass_DataAvailable(string data)
{
Trace.WriteLine("I got some data:\n" + data);
}
}

Jan 16 '06 #2
<jj********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I'm trying to pin down a good naming convention for the 3 things
required to implement an event.


FxCop has a setting where you can audit your naming conventions.

If you don't have it you can get it at

http://www.gotdotnet.com/team/fxcop/

-- Alan
Jan 16 '06 #3
Hello Boz

Not sure you're aware of it, but the 2.0 framework provides a generic
EventHandler.

public event EventHandler<DataAvailableEventArgs> DataAvailable;

I recently refactored a framework for 2.0 and replaced all
eventhandler-specific delegates with the generic 2.0 pattern as seen above.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Jan 16 '06 #4
Thanks for the tip on EventArgs. I'd heard that before and will change
my delegate to follow the conventions.

Cheers

Boz

Jan 16 '06 #5
That looks very handy.

Cheers

Boz

Jan 16 '06 #6
We're not sure we can move to .NET 2.0 yet, but if we do we'll make use
of generics where possible.

Regards

Boz

Jan 16 '06 #7
jj********@yahoo.com wrote:
Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong. Below is my understanding of a naming convention based on the Windows
Forms events. Can someone let me know if this convention is standard,
or suitable or just plain wrong.
I'm not really going to comment on the naming, but:
class ServerClass
{
public event DataAvailableEventHandler DataAvailable;

public FireEvent()
{
if (DataAvailable != null)
{
DataAvailable("bang");
}


Wrong wrong wrong! There is no difference between your

public event DataAvailableEventHandler DataAvailable;

and a

public /*event*/ DataAvailableEventHandler DataAvailable;

That is, DataAvailable is basically a public delegate field, and any
code that sees ServerClass can fire the DataAvailable event and/or
edit the invovation list.

You should be using code like

class ServerClass
{
private event DataAvailableEventHandler dataAvailable;
// Only ServerClass can touch the delegate!

public event DataAvailableEventHandler DataAvailable
// external code can only subscribe/unsubscribe
{
add {dataAvailable += value;}
remove {dataAvailable -= value;}
}

public void FireEvent()
{
if (dataAvailable != null)
{
dataAvailable("bang");
}
}
}

--
<http://www.midnightbeach.com>
Jan 16 '06 #8
As good as that code is, if you try using his method and firing the
event, it wont let you (at least in 2.0 it wont let you).
You should be using code like

class ServerClass
{
private event DataAvailableEventHandler dataAvailable;
// Only ServerClass can touch the delegate!

public event DataAvailableEventHandler DataAvailable
// external code can only subscribe/unsubscribe
{
add {dataAvailable += value;}
remove {dataAvailable -= value;}
}

public void FireEvent()
{
if (dataAvailable != null)
{
dataAvailable("bang");
}
}
}

Jan 17 '06 #9
Ok, I'm confused. Why can't you protect access to event delegate in
..NET 2.0? It makes sense and follows the property/field mechanism.

Boz

Jan 17 '06 #10
Mark Ingram wrote:
As good as that code is, if you try using his method and firing the
event, it wont let you (at least in 2.0 it wont let you).


Aack, you're right! I've been misunderstanding the "event DelegateType
EventName" syntax - noticed that *in class* it acts like a delegate.
Did *not* notice that out-of-class it acts like add/remove delegate.

Thanks!

--
<http://www.midnightbeach.com>
Jan 17 '06 #11

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

Similar topics

3
by: boxboy | last post by:
I am hoping someone from Microsoft could shed some insight into this question. Why did microsoft decide to use a verb based naming convtion for events rather such as Close and Click for rather...
15
by: kode | last post by:
im having problems trying to understand the delegates in c#, does anybody know some link where i can find a good and simple explanation? thanks
15
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple...
4
by: Jarod_24 | last post by:
What is the point with Delegates in VB.Net What can these things do that we can not allready do with the use of Interfaces, Events and Event handlers and so on... I'd like a discussion on this,...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
6
by: Jon Davis | last post by:
I've used delegates fairly heavily for several years in C# for event handling and for starting threads. Does anyone have any real-world scenarios where delegates were both extremely useful and...
114
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
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: 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?
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:
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.