Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
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 
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 
It's always a pleasure to have such a fine conversation with you.

BillWoodruff wrote:
However, I will point out that the implementation of a revised EventArgs class, and using it, will also require changes to each place in your code that raises the Event: to initialize the new parameters.

I'm sorry that I have to contradict you on this matter. You only need to change the code where you actually want to access the values of the EventArgs parameter.

To underline my point I've included an example that illustrates my point. The text in bold mark the changes in comparison to the previous code block.

Basic implementation using the standard EventHandler
C#
class Program {
    static void Main(string[] args) {
        var x = new MySender();
        x.MyEvent += MyHandler;
    }
    
    private static void MyHandler(object sender, EventArgs eventArgs) {
        // Do Stuff
    }
}
public class MySender {
    public event EventHandler MyEvent;    
    // Event Invocators etc.
}


Change the definition of the EventHandler to custom EventArgs doesn't require you to change the code of the handler.
Make sure to derive the Custom EventArgs class from EventArgs or any other class that derives from EventArgs.
C#
class Program {
    static void Main(string[] args) {
        var x = new MySender();
        x.MyEvent += MyHandler;
    }
    
    private static void MyHandler(object sender, EventArgs eventArgs) {
        // Do Stuff
    }
}
public class MySender {
    public event EventHandler<CustomArgs> MyEvent;    
    // Event Invocators etc.
}
public class CustomArgs : EventArgs {
    public int MyInt { get; set; }
}


Change the definition of the handler to access the EventArgs properties
C#
class Program {
    static void Main(string[] args) {
        var x = new MySender();
        x.MyEvent += MyHandler;
    }
    
    private static void MyHandler(object sender, CustomArgs eventArgs) {
        // Do Stuff
        var x = eventArgs.MyInt;
    }
}
public class MySender {
    public event EventHandler<CustomArgs> MyEvent;    
    // Event Invocators etc.
}
public class CustomArgs : EventArgs {
    public int MyInt { get; set; }
}


Cheers, Nicholas
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 
GeneralRe: C# Window form application not working another computer Pin
Richard MacCutchan8-Oct-14 4:45
mveRichard MacCutchan8-Oct-14 4:45 
AnswerRe: C# Window form application not working another computer Pin
Swinkaran8-Oct-14 12:53
professionalSwinkaran8-Oct-14 12:53 

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.