Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an eventhandler as a method argument

Tags:

c#

delegates

I'm trying to pass an event handler to one of my method. This might look stupid but I can't seem to find a way to do it.

I have the red few questions here around on SO but I still don't get over it.

I have the following method signature

public void SyncWithInfo(Action<object, EventArgs> cellValueChanged,
    int indexValeurFr, int indexValeurEn, int indexUnite)

And where I'm trying to call it using this piece of code

private void myMethod()
{
    dgvform.SyncWithInfo(dgvform_CellValueChanged, valeur.Index,
        valeurEN.Index, unite.Index);
}

private void dgvform_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    //stuff
}

I have the following error :

cannot convert from 'method group' to 'System.Action<object, System.EventArgs>'

From what I have red somewhere here on SO refering a method directly by its name without any parameter is considered a 'method group' since there could be X overload of that method.

So here is my question : How can I pass my event handler as an argument for my method?

like image 327
Rémi Avatar asked Jul 28 '26 05:07

Rémi


1 Answers

I think that the SyncWithInfo signature has to use the explict type for EventArg. so that the method signature would look like:

public void SyncWithInfo(Action<object, DataGridViewCellEventArgs> cellValueChanged,
    int indexValeurFr, int indexValeurEn, int indexUnite)
like image 155
kmacdonald Avatar answered Jul 30 '26 17:07

kmacdonald