473,394 Members | 1,724 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,394 software developers and data experts.

passing a method

Is there a way to pass a method as a paramenter when you call a method,
kinda like you do when you create an event handler.
Basically I want to be able to call method A() and tell it to call method
B() but sometimes I want it to call method C(). I know how to design this if
I use interfaces or abstract classes, B() and C() will share signature, and
will most likely be members of similar classes, so using interfaces or
abstract classes is a choice, I just want to know if there is a way to pass
a method as a parameter as an alternative choice.
Nov 15 '05 #1
3 4449
look up System.Delegate

Example:

using System;
public class SamplesDelegate {

// Declares a delegate for a method that takes in an int and returns a
String.
public delegate String myMethodDelegate( int myInt );

// Defines some methods to which the delegate can point.
public class mySampleClass {

// Defines an instance method.
public String myStringMethod ( int myInt ) {
if ( myInt > 0 )
return( "positive" );
if ( myInt < 0 )
return( "negative" );
return ( "zero" );
}

// Defines a static method.
public static String mySignMethod ( int myInt ) {
if ( myInt > 0 )
return( "+" );
if ( myInt < 0 )
return( "-" );
return ( "" );
}
}

public static void Main() {

// Creates one delegate for each method.
mySampleClass mySC = new mySampleClass();
myMethodDelegate myD1 = new myMethodDelegate( mySC.myStringMethod );
myMethodDelegate myD2 = new myMethodDelegate(
mySampleClass.mySignMethod );

// Invokes the delegates.
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 5, myD1( 5 ),
myD2( 5 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", -3,
myD1( -3 ), myD2( -3 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 0, myD1( 0 ),
myD2( 0 ) );
}

}
/*
This code produces the following output:

5 is positive; use the sign "+".
-3 is negative; use the sign "-".
0 is zero; use the sign "".
*/
"memememe" <[rem]casolorz[rem]@hot[rem]mail.com> wrote in message
news:6c***************@news2.central.cox.net...
Is there a way to pass a method as a paramenter when you call a method,
kinda like you do when you create an event handler.
Basically I want to be able to call method A() and tell it to call method
B() but sometimes I want it to call method C(). I know how to design this if I use interfaces or abstract classes, B() and C() will share signature, and will most likely be members of similar classes, so using interfaces or
abstract classes is a choice, I just want to know if there is a way to pass a method as a parameter as an alternative choice.

Nov 15 '05 #2
100
Hi,

Delegates are what you are looking for.

You can pass a delegate as a parameter and then call the targed method
through this delegate.

HTH
B\rgds
100

"memememe" <[rem]casolorz[rem]@hot[rem]mail.com> wrote in message
news:6c***************@news2.central.cox.net...
Is there a way to pass a method as a paramenter when you call a method,
kinda like you do when you create an event handler.
Basically I want to be able to call method A() and tell it to call method
B() but sometimes I want it to call method C(). I know how to design this if I use interfaces or abstract classes, B() and C() will share signature, and will most likely be members of similar classes, so using interfaces or
abstract classes is a choice, I just want to know if there is a way to pass a method as a parameter as an alternative choice.

Nov 15 '05 #3
yeap, thats exactly what I want, I hadn't thought about using delegates like
that, makes perfect sense :-)

"Erik Frey" <er*******@hotmail.com> wrote in message
news:uN**************@tk2msftngp13.phx.gbl...
look up System.Delegate

Example:

using System;
public class SamplesDelegate {

// Declares a delegate for a method that takes in an int and returns a
String.
public delegate String myMethodDelegate( int myInt );

// Defines some methods to which the delegate can point.
public class mySampleClass {

// Defines an instance method.
public String myStringMethod ( int myInt ) {
if ( myInt > 0 )
return( "positive" );
if ( myInt < 0 )
return( "negative" );
return ( "zero" );
}

// Defines a static method.
public static String mySignMethod ( int myInt ) {
if ( myInt > 0 )
return( "+" );
if ( myInt < 0 )
return( "-" );
return ( "" );
}
}

public static void Main() {

// Creates one delegate for each method.
mySampleClass mySC = new mySampleClass();
myMethodDelegate myD1 = new myMethodDelegate( mySC.myStringMethod );
myMethodDelegate myD2 = new myMethodDelegate(
mySampleClass.mySignMethod );

// Invokes the delegates.
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 5, myD1( 5 ), myD2( 5 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", -3,
myD1( -3 ), myD2( -3 ) );
Console.WriteLine( "{0} is {1}; use the sign \"{2}\".", 0, myD1( 0 ), myD2( 0 ) );
}

}
/*
This code produces the following output:

5 is positive; use the sign "+".
-3 is negative; use the sign "-".
0 is zero; use the sign "".
*/
"memememe" <[rem]casolorz[rem]@hot[rem]mail.com> wrote in message
news:6c***************@news2.central.cox.net...
Is there a way to pass a method as a paramenter when you call a method,
kinda like you do when you create an event handler.
Basically I want to be able to call method A() and tell it to call method B() but sometimes I want it to call method C(). I know how to design
this if
I use interfaces or abstract classes, B() and C() will share signature,

and
will most likely be members of similar classes, so using interfaces or
abstract classes is a choice, I just want to know if there is a way to

pass
a method as a parameter as an alternative choice.


Nov 15 '05 #4

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

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
2
by: craigkenisston | last post by:
Hi, I created an array of objects like this : object Values = {myObject.myprop, otherobject.otherprop, thirdobject.xprop}; Then I pass it to a method. and I get the values filled in that...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
5
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.