I have a function with a parameter and i want to call it as a parameter into another function using Action . this is the code :
public void msg(string name)
{
MessageBox.Show("Hello " + name);
}
public void CallMethod(Action<object> Function)
{
Function();
}
it gives me an error when call Method function with msg parameter :
private void Form1_Load(object sender, EventArgs e)
{
CallMethod(msg("John"));
}
I don't want to send the parameter in Method function like this :
public void CallMethod(Action<object> Function)
{
Function("John");
}
any help?
CallMethod(msg("Jhon"))you are passing the result of callingmsg("Jhon")into theCallMethodmethod. The result ofmsg("Jhon")isvoid- i.e. nothing. So it doesn't make sense. Question...why do you want/need to do this? If we understand the overall purpose of trying to create thisCallMethodmethod, then we might be able to suggest a good way to achieve it. At the moment it doesn't seem to do anything useful, because you could just call the same method directly.CallMethod? You need parameter, if you don't want to hardcode"Jhon", but why not callingmsg("Jhon")directly?