Pass extra parameters to an event handler?

Andy Hin picture Andy Hin · Nov 18, 2010 · Viewed 87.8k times · Source

Let's say I want to pass some extra data when assigning an event handler. Consider the following code:

private void setup(string someData)
{
     Object.assignHandler(evHandler);
}

public void evHandler(Object sender)
{
    // need someData here!!!
}

How would I go about getting someData into my evHandler method?

Answer

spender picture spender · Nov 18, 2010
private void setup(string someData)
{
     Object.assignHandler((sender) => evHandler(sender,someData));
}
public void evHandler(Object sender, string someData)
{
    // need someData here!!!
}