Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen9-Oct-14 9:04
professionalEddy Vluggen9-Oct-14 9:04 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff9-Oct-14 11:13
professionalBillWoodruff9-Oct-14 11:13 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:38
professionalEddy Vluggen10-Oct-14 7:38 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Richard Deeming9-Oct-14 22:20
mveRichard Deeming9-Oct-14 22:20 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Eddy Vluggen10-Oct-14 7:50
professionalEddy Vluggen10-Oct-14 7:50 
AnswerRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty10-Oct-14 5:24
professionalNicholas Marty10-Oct-14 5:24 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff10-Oct-14 11:23
professionalBillWoodruff10-Oct-14 11:23 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 1:13
professionalNicholas Marty13-Oct-14 1:13 
BillWoodruff wrote:
But, I guess if I were part of a team, on a large, complex, project, implementing Events in the usual way with custom EventArgs would probably pay off in the long-run. Possibly contributing to code consistency and maintainability ? Flexibility ?


I think using the EventHandler pattern for all events induces some consistency at the least, because you can always subscribe to it in the same way. You don't need to do anything with the sender or the args parameter if you don't need to (but you can if you want).

Now lets consider a scenario where you just want to raise an event and react to that event in a very specific way. You don't need any parameters in that case at first but need to add parameters later (which you don't know when first implementing this)

Using the action
You use a simple Action to handle this event. You attach a handler and everything works fine. So far so good. Now you suddenly have the requirement to add a parameter to this value... uh.. alright. Change the signature of the Action to include the parameter and update every usage of it too... another parameter: same game again.

Using the EventHandler pattern
You use a standard EventHandler to Handle this event. Again, everything works fine. When the need arises to pass some arguments to the handler you can just change the signature of the EventHandler to include a custom EventArgs parameter. Without updating any references to it, everything is still working (at least if your parameter derives from System.EventArgs) now you can change the signature of the method that handles the event and access the fields/properties of that parameter to do something based on that value. Another parameter. Just add an additional field and 'lo and behold: you didn't break any existing handler Smile | :)

Sure you can use the action to pass exactly one parameter with a data object the likes of an EventArgs parameter, but why go to that length when you could have used the EventHandler in the first place?

With the EventHandler you're pretty flexible in terms of adding (additional) arguments (or to remove some if you check if nobody actually uses the ones you want to remove) without having a lot of work to update any existing code. Sure it might seem like a bit of an overhead when writing it initially or a bit unnecessary to pass arguments that your handler doesn't actually need to process the event, but the coupling of those is a bit more loose than hard-wiring the actual method signature. I think even when working alone this might contribute to code that's easier to understand.

Of course, everything is very subjective for each individual programmer. Maybe for code you write and use only yourself it doesn't really matter. But maybe you have a public interface that is used by others or you see code that you've written a few years ago (of course there's alway the proper documentation available, right? Laugh | :laugh: ) So it might be wise to stick to a consistent style and maybe to follow a style that other do too, in case you have to use code written by others.
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
BillWoodruff13-Oct-14 1:34
professionalBillWoodruff13-Oct-14 1:34 
GeneralRe: using "naked" Action/Func as EventHandlers ? C# language question Pin
Nicholas Marty13-Oct-14 2:17
professionalNicholas Marty13-Oct-14 2:17 
QuestionTransaction in active directory Pin
Nitin K8-Oct-14 23:21
Nitin K8-Oct-14 23:21 
AnswerRe: Transaction in active directory Pin
Eddy Vluggen9-Oct-14 0:30
professionalEddy Vluggen9-Oct-14 0:30 
QuestionHow to make custom pagination when use grid view in c# Pin
heyvid8-Oct-14 21:10
heyvid8-Oct-14 21:10 
AnswerRe: How to make custom pagination when use grid view in c# Pin
Richard MacCutchan8-Oct-14 21:42
mveRichard MacCutchan8-Oct-14 21:42 
AnswerRe: How to make custom pagination when use grid view in c# Pin
Maciej Los9-Oct-14 10:56
mveMaciej Los9-Oct-14 10:56 
GeneralHow to Create task bar in c# windows app Pin
Srikanth598-Oct-14 3:51
Srikanth598-Oct-14 3:51 
GeneralRe: How to Create task bar in c# windows app Pin
DaveAuld8-Oct-14 3:58
professionalDaveAuld8-Oct-14 3:58 
JokeRe: How to Create task bar in c# windows app Pin
Duncan Edwards Jones8-Oct-14 4:04
professionalDuncan Edwards Jones8-Oct-14 4:04 
GeneralRe: How to Create task bar in c# windows app Pin
enhzflep8-Oct-14 3:59
enhzflep8-Oct-14 3:59 
GeneralRe: How to Create task bar in c# windows app Pin
Richard MacCutchan8-Oct-14 4:19
mveRichard MacCutchan8-Oct-14 4:19 
QuestionC# Window form application not working another computer Pin
coolsuhail8-Oct-14 3:39
coolsuhail8-Oct-14 3:39 
AnswerRe: C# Window form application not working another computer Pin
Dominic Burford8-Oct-14 4:00
professionalDominic Burford8-Oct-14 4:00 
GeneralRe: C# Window form application not working another computer Pin
coolsuhail10-Oct-14 1:47
coolsuhail10-Oct-14 1:47 
AnswerRe: C# Window form application not working another computer Pin
Richard MacCutchan8-Oct-14 4:17
mveRichard MacCutchan8-Oct-14 4:17 
GeneralRe: C# Window form application not working another computer Pin
Pete O'Hanlon8-Oct-14 4:35
mvePete O'Hanlon8-Oct-14 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.