Click here to Skip to main content
15,916,398 members
Home / Discussions / C#
   

C#

 
AnswerRe: create Web browser in C# without using web browser control Pin
Pete O'Hanlon10-Oct-14 9:56
mvePete O'Hanlon10-Oct-14 9:56 
Questionhi friends Pin
Member 111430869-Oct-14 22:52
Member 111430869-Oct-14 22:52 
AnswerRe: hi friends Pin
Eddy Vluggen10-Oct-14 7:51
professionalEddy Vluggen10-Oct-14 7:51 
QuestionPlaying media with 2 different channels in stereo file using NAudio library Pin
Praveen Raghuvanshi9-Oct-14 17:55
professionalPraveen Raghuvanshi9-Oct-14 17:55 
AnswerRe: Playing media with 2 different channels in stereo file using NAudio library Pin
Eddy Vluggen10-Oct-14 7:53
professionalEddy Vluggen10-Oct-14 7:53 
QuestionWinform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 1:56
professionalDerek Kennard9-Oct-14 1:56 
AnswerRe: Winform Change Label Text from secondary form action Pin
BillWoodruff9-Oct-14 3:00
professionalBillWoodruff9-Oct-14 3:00 
GeneralRe: Winform Change Label Text from secondary form action Pin
Derek Kennard9-Oct-14 15:28
professionalDerek Kennard9-Oct-14 15:28 
QuestionMake Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 1:28
avisharma@sharma9-Oct-14 1:28 
AnswerRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
Simon_Whale9-Oct-14 1:36
Simon_Whale9-Oct-14 1:36 
GeneralRe: Make Cell Read Only after clicking the Cell in DataGridView Pin
avisharma@sharma9-Oct-14 2:05
avisharma@sharma9-Oct-14 2:05 
Questionusing "naked" Action/Func as EventHandlers ? C# language question PinPopular
BillWoodruff9-Oct-14 1:07
professionalBillWoodruff9-Oct-14 1:07 
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 

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.