Re: mucking with Event Definitions to get tighter coupling to Objects ?



On Sun, 03 Feb 2008 10:31:21 -0800, Bill Woodruff <msnwsgroups@xxxxxxxxxxxxxx> wrote:

[...]
1. is what I am doing flat-out wrong/bad/blasphemy/poor coding in your
opinion ?

No, I wouldn't say so.

2. what issues could arise from not following the standard .NET event
Sender, EventArgs parameter order structure. I'm assuming there are issues
other than the developer has to carry a slightly more "heavy" weight mental
model.

The primary issue I see is the one that motivates the .NET convention: if you make the event delegate signature dependent on the class implementing the event, then you cannot write a handler that is compatible with multiple classes, generally speaking. One exception is if the event is declared in a base class, then of course you could have a single event handler method handle the same event for multiple derived classes.

This doesn't come up a lot, but it can. If it's of no concern to you, then I wouldn't worry about it too much.

The other issue is of course adhering to existing conventions. This is not one I personally worry too much about, but I know that others do and I don't fault them for it. This isn't a code-correctness issue so much as it is a potential maintenance and multi-person team issue. Having the exact same signature pattern for all events is helpful in making the code readable, because you can always look at a method and see right away that it's an event handler. That said, there are other clues in the method signature (for example, if the first parameter is always named "sender" and the second parameter's type always has the phrase "EventArgs" in it, then that's at least as powerful a clue).

3. if you would use a technique like this, in what scenarios would you use
it and why.

I can't think of any in which I'd specifically bother. That said, I'm sure that at least once I've written an event that doesn't share _any_ of the .NET convention (e.g. may not even include a sender or event data). It can be done, and if you feel it's important enough to do so, then don't let the convention stand in your way.

4. could this technique, by eliminating casts and lookups contribute any
real increase in speed or whatever in apps ? In other words, given you Can
use it, is it worth using ?

Not from a performance point of view, no. If your code's performance is bottlenecked by a cast, then it is already doing basically nothing and should be able to do that nothing extremely quickly. :)

many thanks for your examining and commenting on this. the code has
unusually long verbose variable names because I am writing it as an
educational tool to demonstrate an alternate way to Raise Events ...

I didn't even bother to look closely at the code. For what it's worth, if you're asking a question, post code that someone will actually read, and which includes _only_ the bare minimum required. In this particular instance, you could have simply posted the .NET convention, and an example of the delegate type for a non-compliant event. There was no need to post all that other stuff, and the more complex the code you post, the more likely someone will not read it, or will even ignore your entire post.

Pete
.



Relevant Pages

  • Re: How to pass more than two parameters in the event handler
    ... // Create EventArgs subclass to pass in 2D array parameter ... public intcurveArray; ... public delegate void getCurveButtonHandler(object sender, int ... my event handler getCurveButtonHandler but seems like this is not the best ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Directred Event Multicasting
    ... >> It would be way cool if it were possible for, say, a single event handler ... >> trap the Clicked events for all buttons and, then, re-route those events ... > (which it would normally do with the "sender" argument), ... nice to have, in addition to the (sender, eventArgs) semantics, something ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: autosave problems
    ... in the event handler ... Private void Sheet1_Buttonclick(object sender, System.EventArgs e) ... Alvin Bruney ... Could you please give me a hint on which tag property you ar talking ...
    (microsoft.public.vsnet.vstools.office)
  • RE: Tab Stops in DataGrid
    ... In the event handler, move the CurrentCell to the next cell in the grid. ... private void Form1_Load(object sender, System.EventArgs e) ... before sending mail. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: addhandler and creating buttons at run time
    ... The DirectCast statement simply converts the "sender" variable (which is ... get Button1 passed to the Click event handler as an object. ... >> Nice that you wrote you are a student, than we can take the students ... >> can add all the time the same event code to the control. ...
    (microsoft.public.dotnet.languages.vb)

Loading