By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

send function name as parameter to other function

P: n/a
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.

Apr 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Er********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
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.

Apr 10 '07 #2

P: n/a
On Apr 10, 5:22 pm, Eran.Ya...@gmail.com wrote:
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.
Apr 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.