views:

134

answers:

3

I am wondering if it is possible (and what the syntax would be) to send an object's method to a function.

Example:

Object "myObject" has two methods "method1" and "method2"

I would like to have a function along the lines of:

public bool myFunc(var methodOnObject)
{
   [code here]
   var returnVal = [run methodOnObject here]
   [code here]
   return returnVal;
}

So that in another function I could do something like

public void overallFunction()
{
   var myObject = new ObjectItem();
   var method1Success = myFunc(myObject.method1);
   var method2Success = myFunc(myObject.method2);
}
+2  A: 

Yes, using delegates ..

Here is an example..

delegate string myDel(int s);
public class Program
{
    static string Func(myDel f)
    {
        return f(2);
    }

    public static void Main()
    {
        Test obj = new Test();
        myDel d = obj.func;
        Console.WriteLine(Func(d));
    }
}
class Test
{
    public string func(int s)
    {
        return s.ToString();
    }
}
Svetlozar Angelov
Providing an example will probably give you quite some up-votes...
Stefan Steinegger
+8  A: 

Yes, you need to use a delegate. Delegates are fairly analogous to function pointers in C/C++.

You'll first need to declare the signature of the delegate. Say I have this function:

private int DoSomething(string data)
{
    return -1;
}

The delegate declaration would be...

public delegate int MyDelegate(string data);

You could then declare myFunc in this way..

public bool myFunc(MyDelegate methodOnObject)
{
    [code here]
    int returnValue = methodOnObject("foo");
    [code here]
    return returnValue;
}

You can then call it in one of two ways:

myFunc(new MyDelegate(DoSomething));

Or, in C# 3.0 and later, you can use the shorthand of...

myFunc(DoSomething);

(It just wraps the provided function in the default constructor for that delegate automatically. The calls are functionally identical).

If you don't care to actually create a delegate or actual function implementation for simple expressions, the following will work in C# 3.0 as well:

public bool myFunc(Func<string, int> expr)
{
    [code here]
    int returnValue = methodOnObject("foo");
    [code here]
    return returnValue;
}

Which could then be called like so:

myFunc(s => return -1);
Adam Robinson
C# 3.5 also lets you avoid declaring your own delegate type, as you can do `Func<string, int>` which represents the same signature as your `MyDelegate` type.
Simon Steele
@Simon: In some cases, yes, there are some built-in delegate types, and 3.5 did add several potentially useful ones as part of the LINQ support package. That being said, being able to declare a delegate is something that, IMO, any .NET developer should be able to do.
Adam Robinson
Great answer, thanks for all the information and details. Voted up, but used other as offical answer as it directly used my example. Sometimes wish there could be 2 accepted answers as combined your two answers give all the details anyone could want for this question. Thanks again!
ChrisHDog
+3  A: 

Is there really a need for explicit delegates? Maybe this approach would help you:

private class MyObject
{
    public bool Method1() { return true; } // Your own logic here
    public bool Method2() { return false; } // Your own logic here
}

private static bool MyFunction(Func<bool> methodOnObject)
{
    bool returnValue = methodOnObject();
    return returnValue;
}    

private static void OverallFunction()
{
    MyObject myObject = new MyObject();

    bool method1Success = MyFunction(myObject.Method1);
    bool method2Success = MyFunction(myObject.Method2);
}
Seb Nilsson
Not sure if that is a typo... `Func<...>` very much **is** a `delegate`.
Marc Gravell
Sorry for deleting my comment, it was about "Is there really a need for delegates?" and making an example with Func which is a delegate...
Svetlozar Angelov
That is great, thanks. I accepted this as the answer as it used my example directly.
ChrisHDog
Marc Garvell: I meant explicitly using the keyword delegate. Using a Func<...> is more comprehendable and nice syntactic sugar.
Seb Nilsson