Passing Function as Argument

  • Thread starter Thread starter John Smith
  • Start date Start date
J

John Smith

Hello all:

Is there a way to pass a function address to another function in C#?

I have a few event handler functions such as menuItem_Click that I would
like to pass as such TheFunction(menuItem_Click), and receive as such
private void AnotherFunction(?what goes here?).

Thanks.
 
Hello all:

Is there a way to pass a function address to another function in
C#?

I have a few event handler functions such as menuItem_Click that
I would like to pass as such TheFunction(menuItem_Click), and
receive as such private void AnotherFunction(?what goes here?).

John,

Look up "delegate" in the help file.
 
Think in objects. What objects should be called by your method? menu items
only?

using System.Windows.Forms;

//Called function
void MyMethod(MenuItem useThisControl)
{
// Somewhere in here,
useThisControl.PerformClick();
}

//Calling code
// ...
MyObject.MyMethod( menuItem );

For more info
http://msdn.microsoft.com/library/d...indowsformsmenuitemclassperformclicktopic.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top