Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,
I am having a hard time with passing a function name as a parameter to another function.
I have the following setup:
1. There are 2 classes (A and B)
2. I am trying to write a function in class A which would be executing methods from class B.

I know I can pass a class instance and then run the method from that instantiated class... but I just can find a way to also pass a method name as well (don't want to type the name manually).
I found a good example over here:
http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp[^]
But the problem is that this example shows how to pass method name of the same class (in other words you can access the method directly without having to instantiate the class).... in my case it's a bit different story - I have to instantiate the class....

Thank you in advance guys!

Modestas
Posted
Comments
Tom Wauters 27-Jun-13 0:33am    
Is it a possibility to make the methods in class B static?
MK-Gii 27-Jun-13 0:48am    
I think it is... what would be the approach here?
johannesnestler 27-Jun-13 10:32am    
Hmm, after some thinking I can not see why you can't just create one/multiple delegates (function pointer types) and just give the function pointer (pointing to the functions of B) to the calling method in A. Is this a missunderstanding on my side? In the end you try to solve kind of "changable algorithm" problem, don't you? (method in A do need to do calculation B.1 if criteria xy is met, or do calculation B.2 for yy, ...)

Hello Everyone, hello Tom,
I got this solved and the link you gave me was very uselfull (the for delegates in MSDN)

The solution for this was that I passed delegate to the function instead of passing the function name. And before that I assign the function to delegate like this:

C#
private delegate void my_delegate(string name); //create delegate that takes string as an argument

Class_A class_instance = new Class_A(); //create class instance
my_delegate = class_instance.Function_I_want_to_Pass; //assign the delegate

//now I can use this:
private void Function_Caller(argument_1, argument_2, my_delegate)
{
    //do something with argument #1 and argument #2
    //.......
    //call the function (delegated)
    my_delegate("Modestas");
}
//closing the class instance
class_instance.Close();


now I can pass the delegate to the function (so it's just like passing the function name).
Of course - this way I create few more lines in the code but this does not bother as I can still achieve the goal of universal function caller.

Hope this helps anyone.
Thanks a lot!

Modestas.
 
Share this answer
 
v3
Hi,

I do not know if this will help, but you could use reflection to execute a method off of a class.

http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters[^]

So if you instantiate an instance of class b then use reflection to find which ever method name has been passed into the method on class A it might achieve what you want.

Although it's performance hit might be substantial.
 
Share this answer
 
In ClassB
C#
public static void doStuffInB()
       {
       //do some really cool stuff.
       }


in ClassA:
C#
private void Form1_Load(object sender, EventArgs e)
        {

            ClassB.doStuffInB();
        }
 
Share this answer
 
Comments
MK-Gii 27-Jun-13 1:14am    
Thanks for reply mate, but I need to pass this in some other way:
I need to pass function name as an argument.. so it should look something like this:
private void FunctionInClassA(doStuffInB)

This I only need because I want to reuse code. Class B has multiple functions and in class A function I want to pass function name from class B and few other parameters. So in other words it would be something like universal function in class A.

Thanks!
Tom Wauters 27-Jun-13 1:29am    
Maybe Delegates are the answer
http://msdn.microsoft.com/en-us/library/ms173172(v=vs.80).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900