send function name as parameter to other function

  • Thread starter Thread starter Eran.Yasso
  • Start date Start date
E

Eran.Yasso

Hi,

I saw that there's the delegate feature that handles as pointer to
function but in safe mode.
Can I send function name to other function to call it?

for example:

printmeA()
{
Console.WriteLine( "printmeA()");
}
printmeB()
{
Console.WriteLine( "printmeB()");
}
printmeC()
{
Console.WriteLine( "printmeC()");
}
function B(object Calledfun)
{
Calledfun();
}
function A()
{
B(printmeA);
B(printmeB);
B(printmeB);
}

thanks.
 
Eran,

Well, you can have a take a list of delegate to call, like so:


void B(MethodInvoker function)
{
function();
}

void A()
{
B(printmeA);
B(printmeB);
B(printmeB);
}

MethodInvoker is a delegate defined in the System.Windows.Forms
namespace. If you don't want to load that assebly just for that, you can
declare your own delegate:

delegate void NameThisWhatYouWant();

Hope this helps.
 
Hi,

I saw that there's the delegate feature that handles as pointer to
function but in safe mode.
Can I send function name to other function to call it?

for example:

printmeA()
{
Console.WriteLine( "printmeA()");}

printmeB()
{
Console.WriteLine( "printmeB()");}

printmeC()
{
Console.WriteLine( "printmeC()");}

function B(object Calledfun)
{
Calledfun();}

function A()
{
B(printmeA);
B(printmeB);
B(printmeB);

}

thanks.

Ok just found the solution. Hope It is ok how I did it:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class AsyncMain
{
private delegate void WriteMeCallback( string Message );

private static void printmeA( string msg )
{
Console.Write( "printmeA()" + msg );
}
private static void printmeB( string msg )
{
Console.Write( "printmeB()" + msg );
}
private static void printmeC( string msg )
{
Console.Write( "printmeC()" + msg );
}
private static void callb( WriteMeCallback d,string msg )
{
d.Invoke( msg );
}
public static void Main()
{
WriteMeCallback d = new WriteMeCallback( AsyncMain.printmeA );
callb( d,"1" );
d = new WriteMeCallback( AsyncMain.printmeB );
callb( d , "2" );
d = new WriteMeCallback( AsyncMain.printmeC );
callb( d , "3" );
}
}
}

my question is if the line "d = new
WriteMeCallback( AsyncMain.printmeB );" is correct.
 

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