473,785 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Style Question: Event handling

The canonical way of declaring delegates for events is to include a
parameter representing the sender as well as an EventArgs derived class
(or EventArgs itself) as the second parameter representing the data that
accompanies the event. For example:

public delegate void MessageReceived Handler(object sender,
MessageReceived Args e);

// ...

public event MessageReceived Handler MessageReceived ;

Event handlers are usually declared as private or protected. Using the
above example, the method for handling the MessageReceived event would
look like this:

private void HandleMessageRe ceived(object sender,
MessageReceived EventArgs e)
{
// ...
}

A few of questions:

Would it be considered bad style to make the event handler method
public?

The reason I ask is that it would be nice to have a third party wire up
objects via events and event handlers and let the objects talk to each
other without having to know anything about each other other than the
delegate signature.

// Class A has an event of the MessageReceived Handler type.
A a = new A();

// Class B has a public event handler matching the
MessageReceived Handler signature.
B b = new B();

a.MessageReceiv ed +=
new MessageReceived Handler(b.Handl eMessageReceive d);

Assuming this is ok so far, I think it would be nice to change the name
of the event handler from HandleMessageRe ceived to something a little
less verbose and more straight forward. Ideally the method's name would
reflect what it does, say in this example it sends the event's data
somewhere. So let's call it Send.

a.MessageReceiv ed += new MessageReceived Handler(b.Send) ;

Since the Send method is public, it can be called directly instead of in
response to an event. However, the sender parameter may not be necessary
and just adds noise to the call, so let's get rid of it. Also, instead
of the EventArgs derived class parameter, let's pass the data directly.
Say in this case it is some fictional Message class object. So we now
have this:

public delegate void MessageReceived Handler(Message msg);

// ...

// Declared in class A.
public event MessageReceived Handler MessageReceived ;

A a = new A();
B b = new B();
Message msg = new Message();

a.MessageReceiv ed += new MessageReceived Handler(b.Send) ;

// Event handler called directly.
b.Send(msg);

I realize that I've gone further and further away from Microsoft's .NET
coding guidlines for events. However, my point is that it would be nice
to use the event/delegate mechanism to set up a kind of pipe and filter
architecture between objects and that the guidlines may be adding extra
noise that isn't necessary in this case.

I don't ignore guidelines lightly, and was just wondering what
everyone's thought on this was. At which point do you draw the line?
When do you think it's appropriate to bypass the guidelines? Is it
appropriate in the case I described above?

Nov 17 '05 #1
2 1942

"Wavemaker" <ja**********@B iteMeHotmail.co m> wrote in message
news:hu******** ************@co mcast.com...
Would it be considered bad style to make the event handler method
public?
I don't see why (why it would be bad, that is).
Since the Send method is public, it can be called directly instead of in
response to an event. However, the sender parameter may not be necessary
and just adds noise to the call, so let's get rid of it.
Here I'm getting skeptical. The "sender" parameter may seem unnecessary now,
but will it ever be necessary at some time in the future? You're limiting
your functionality in that the object handling the event can now never know
what object generated the event.
Also, instead
of the EventArgs derived class parameter, let's pass the data directly.
Say in this case it is some fictional Message class object.
This also has a bad smell. I would derive from EventArgs and add a property
to carry the Message object.
I realize that I've gone further and further away from Microsoft's .NET
coding guidlines for events. However, my point is that it would be nice
to use the event/delegate mechanism to set up a kind of pipe and filter
architecture between objects and that the guidlines may be adding extra
noise that isn't necessary in this case.

I don't ignore guidelines lightly, and was just wondering what
everyone's thought on this was. At which point do you draw the line?
When do you think it's appropriate to bypass the guidelines? Is it
appropriate in the case I described above?


The question I would ask is this: When you hire a new programmer and assign
them a task involving working with what you've described, how long will it
take them to "get it"? It seems pretty straight-forward to me, but it is
different enough that it is still foreign.

If it were me, I would tend to stick with the "recommende d" method and have
two public methods:

public void Send(Message msg)
{
}

public void HandleMessageEv ent(object sender, MessageEventArg s e)
{
Send(e.Message) ;

// Will I ever need to send a response back to "sender"?
}

Now you can essentially call "Send()" either way (directly or via an event)
and it's pretty clear what's going on either way.
Nov 17 '05 #2

"Scott Roberts" wrote:
"Wavemaker" wrote in message:


<snip bypassing Microsoft's guidelines for coding events>
I realize that I've gone further and further away from Microsoft's
.NET coding guidlines for events. However, my point is that it would
be nice to use the event/delegate mechanism to set up a kind of pipe
and filter architecture between objects and that the guidlines may be
adding extra noise that isn't necessary in this case.

I don't ignore guidelines lightly, and was just wondering what
everyone's thought on this was. At which point do you draw the line?
When do you think it's appropriate to bypass the guidelines? Is it
appropriate in the case I described above?


The question I would ask is this: When you hire a new programmer and
assign them a task involving working with what you've described, how
long will it take them to "get it"? It seems pretty straight-forward
to me, but it is different enough that it is still foreign.

If it were me, I would tend to stick with the "recommende d" method and
have two public methods:

public void Send(Message msg)
{
}

public void HandleMessageEv ent(object sender, MessageEventArg s e)
{
Send(e.Message) ;

// Will I ever need to send a response back to "sender"?
}

Now you can essentially call "Send()" either way (directly or via an
event) and it's pretty clear what's going on either way.


Ah, the best of both worlds. Honestly, I hadn't thought of that before.
Thanks for a clear and helpful response.

Nov 17 '05 #3

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

Similar topics

2
3374
by: Marinos Christoforou | last post by:
Sorry if this has been asked before but as an inexperienced wanna-be C# programmer I wondering how to code classes to help build a standard Windows UI. For example to build a common toolbar. I would call a method from the class in the load event of my varioius windows forms, to create the toolbar, add buttons to it and set various properties as required. This is fine but I am at a loss as to how to expose the toolbar's various events...
1
2841
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
44
4229
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
4
1754
by: hillcountry74 | last post by:
Hi, I'm a newbie and trying to understand event handling in c#. I have understood handling events using delelgate objects. But not this method- "Event handling by overriding the virtual protected method of the base class". Can someone please explain this with a sample code? Thanks a lot.
2
1510
by: ETL | last post by:
Hi, I think this question has an easy answer but I can't seem to find anything online that lists best practices for my problem. I have a C# app that uses an exception handling tree similar to the following. Try { blah blah blah....
3
1787
by: cmay | last post by:
In reading some documents from the Patterns and Practices group, I had a question about the example given for the Page Controller pattern. (I have posted the code for the BasePage below) In short, their example shows a "BasePage " class for other pages to inherit from. The subclasses are supposed to implement the PageLoadEvent method (which I guess should really be abstract, but can't because asp.net
7
1390
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
3
1408
by: James Ramaley | last post by:
I have created a VB.NET User Control which inherits from TextBox. I have a general question regarding how functionality should be overwritten: I can either write a method which handles an event: function MyControl_Enter(...) handles MyControl.Enter or I can overwrite the relevant virtual method:
3
2003
by: a | last post by:
Hi, I have a 3-column listview. It has to return the value of the cell when the user click the listview. The SelectedIndexChanged event and ItemSelectionChanged Event only return the row information (ListViewItem). If I want to return the cell value of the second and the third column, which event should I use? Let say, if the user select row 2, column 3, which event handling
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6741
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
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 we have to send another system
3
2881
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.