473,772 Members | 3,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne( "printmeA() ");
}
printmeB()
{
Console.WriteLi ne( "printmeB() ");
}
printmeC()
{
Console.WriteLi ne( "printmeC() ");
}
function B(object Calledfun)
{
Calledfun();
}
function A()
{
B(printmeA);
B(printmeB);
B(printmeB);
}

thanks.

Apr 10 '07 #1
2 2898
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 NameThisWhatYou Want();

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<Er********@gma il.comwrote in message
news:11******** *************@o 5g2000hsb.googl egroups.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.WriteLi ne( "printmeA() ");
}
printmeB()
{
Console.WriteLi ne( "printmeB() ");
}
printmeC()
{
Console.WriteLi ne( "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...@gmai l.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.WriteLi ne( "printmeA() ");}

printmeB()
{
Console.WriteLi ne( "printmeB() ");}

printmeC()
{
Console.WriteLi ne( "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.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
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.print meA );
callb( d,"1" );
d = new WriteMeCallback ( AsyncMain.print meB );
callb( d , "2" );
d = new WriteMeCallback ( AsyncMain.print meC );
callb( d , "3" );
}
}
}

my question is if the line "d = new
WriteMeCallback ( AsyncMain.print meB );" is correct.
Apr 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2759
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__ is a special case ). The closest to this I could find was the 2002-12-04 posting 'metaclasses and static methods' by Michele Simionato. The posting's example metaclass uses the method's name. I present my own example of automatic method kind...
9
3697
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
1
3346
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 Function explanation Part II
2
2514
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 pointer or reference to a derived type of the base class's return type. In .NET the overridden virtual function is similar, but an actual parameter of the function can be a derived reference from the base class's reference also. This dichotomy...
7
7742
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 which will be used to query sql server to repopulate the datagrid in the user control. I also wrapped the user control in a panel element so I could position it on the page. I'm clueless on how to get started with this.
6
3854
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 over the lan with another sender name - how can i do this ... is it possible in vb.net? thanks
5
7463
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 used Object class as parameter. Now it can take any object as its parameter. But the problem is that I want to access the values of the private or public member variables of the passed object, for which I may have to typecast the Object class...
4
2384
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. Because I want to use some ajax-requests and other javascript-functions on my xhtml, I need to dynamically add event handlers to any possible dom-elements. All solutions I found so fare are for specific, pre-known
4
2496
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 2006) $ Author: Collin Winter <collinw@gmail.com>, Tony Lownds <tony@lownds.com> Status: Draft Type: Standards Track
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.