Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# delegates
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 26-Jun-13 5:11am
Comments
Tom Wauters at 27-Jun-13 0:33am
   
Is it a possibility to make the methods in class B static?
Modestas Krusa at 27-Jun-13 0:48am
   
I think it is... what would be the approach here?
johannesnestler at 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, ...)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In ClassB
 public static void doStuffInB()
        { 
        //do some really cool stuff.
        }
 
in ClassA:
private void Form1_Load(object sender, EventArgs e)
        {
 
            ClassB.doStuffInB();
        }
 
  Permalink  
Comments
Modestas Krusa at 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 at 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)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 28 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100