This page is a draft and may contain incorrect information and/or experimental styling.
Add parameter to an event trap
The following illustrates how to add an additional parameter to an event trap.Create a method that takes the standard parameters of an Event Handler, plus the extra parameter. In this case, the parameter is an integer.
void MyEventTrapWithParameter(object sender, EventArgs e, int MyExtraParam)
{
// do some stuff here
}
If you want to use your MyEventTrap method with additional argument evaluated during event registration, you can use lambda expression as an adapter:
Our method needs to be specified during event registration. However, the standard Event Handler takes only 2 parameters. Use a lambda expression as an adapter. This can be done in 2 ways.
void Test()
{
// Declare an instance of an integer variable that we want to pass to the event handler.
int lSchID = 5;
// the SchedulerFired event handler expects only 2 parameters. Use a lambda expression as an adapter.
this.sch.SchedulerFired += new EventHandler((sender2, ev) => this.MyEventTrapWithParameter(sender2, ev, lSchID));
// shorthand for the above. Works the same.
this.sch.SchedulerFired += (sender2, ev) => this.MyEventTrapWithParameter(sender2, ev, lSchID);
}
Last modified by Mohit @ 4/5/2025 8:06:14 PM