473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

send function name as parameter to other function

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
2 2874
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
1
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
7
by: moondaddy | last post by:
I have a user control being used instead of a frame page. when the user clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control...
6
by: Thomas | last post by:
hello, i am searching for a while to use net send in vb.net without using the commandline net send... is there an api, with an example... i know that there is the possib. to send such messages...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
4
by: simon | last post by:
hi, I would like to separate my javascript completely from my xhtml. in the end there should be only <script type="text/javascript" src="javalib.js"></script> in the head-tag to my javascript....
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.