nAll right , again one post away from my favorite SharePoint world but this is kind of generic which can be used anywhere in C#.
n
nToday we will see how we can pass some custom parameters to any event in C#.
n
nConsider a scenario where you are calling some method asynchronously and execution is happening in loops.
nIn General, we register an event with the method which gets called after method call is completed. but as the asynchronous execution is happening in the loop , so in the on completed event you never know which for which call the execution has been completed.
n
nCheck out this sample scenario where I have a simple windows forms application with two buttons and label, both buttons are registered to a same on click event. Now say, on click of each button I want to perform some independent actions and also want to pass some parameters to the on click event. I know there can be many approaches to do this but I have just taken this example to prove the actual point of this blog post.
n
n
n
n
nToday we will see how we can pass some custom parameters to any event in C#.
n
nConsider a scenario where you are calling some method asynchronously and execution is happening in loops.
nIn General, we register an event with the method which gets called after method call is completed. but as the asynchronous execution is happening in the loop , so in the on completed event you never know which for which call the execution has been completed.
n
nCheck out this sample scenario where I have a simple windows forms application with two buttons and label, both buttons are registered to a same on click event. Now say, on click of each button I want to perform some independent actions and also want to pass some parameters to the on click event. I know there can be many approaches to do this but I have just taken this example to prove the actual point of this blog post.
n
n
n
npublic Form1()
n
n{
n
n InitializeComponent();
n
n
n
n button1.Click += newnEventHandler(button_Click);
n
n button2.Click += newnEventHandler(button_Click);
n
n}
n
n
n
nvoid button_Click(objectnsender, EventArgs e)
n
n{
n
n nlabel1.Text = “Hi “;
n
n}
n
n
n
nNow on both button clicks – the text of a label is printed as ‘Hi’ as the event is common between both buttons.
n
n
n
nNow what I want is to pass some extra information to the on click event from both buttons , you can pass on any number and type of objects as you want. for the example I will send a string and Enum to click events.
n
n
n
nI will also need to modify the button click event , as I am going to send some parameters to event so I need to catch those in the event.
n
n
n
n
n
npublic Form1()
n
n{
n
n nInitializeComponent();
n
n
n
n nbutton1.Click += delegate(object sender, EventArgsne) { button_Click(sender, e, “This is FromnButton1”, MessageType.B1);n};
n
n
n
n nbutton2.Click += delegate(object sender, EventArgsne) { button_Click(sender, e, “This is FromnButton2”, MessageType.B2);n};
n
n
n
n}
n
n
n
nvoid button_Click(objectnsender, EventArgs e, string message, MessageTypentype)
n
n{
n
n ifn(type.Equals(MessageType.B1))
n
n {
n
n label1.Text = message;
n
n }
n
n else if (type.Equals(MessageType.B2))
n
n {
n
n label1.Text = message;
n
n }
n
n}
n
n
n
nenum MessageType
n
n{
n
n B1,
n
n B2
n
n}
n
n
n
n