Reputation: 11824
Ok, I already passing function as parameter to another function like this:
someFunction(..., passFunc);
passFunc(){ MessageBox.Show("Message"); }
private void someFunction(..., Func<int> f) { f(); }
This works just fine. I pass "passFunc" to "someFunction" as parameter and then call this parameter("f()"). Then I got messagebox with text "message". Ok.
In later version of program, function "passFunc" is called when I click on specific button's...
Then I wish to get text. I normally do this so: ((Button)sender).Text;
. But in this case I cant do this, because I have no parameters in this "passFunc".
How can I do this? So, that "passFunc" will look like: passFunc(object sender, EventArgs e)
Then I'll be able to do this:
passFunc(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
}
here is code: http://pastie.org/6079188
Upvotes: 1
Views: 196
Reputation: 1072
If you want to pass input to a func use this generic type overload:
Func<string, int> passFunc
EDIT:
You call passFunc
like this
passFunc("some input");
EDIT2:
passFunc(object sender, EventArgs e)
cannot be used with a Func because it probably has a void return type.
You then have to use Action<object, EventArgs>
.
Upvotes: 1
Reputation: 572
Why don't you subscribe passFunc to the event handler for that button?
It would look something like this
button1.Click += new EventHandler(passFunc)
and then passFunc would be
void passFunc(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Text);
}
I don't know if this is exactly what you want to achieve but it might work.
Upvotes: 0