tags:

views:

130

answers:

4

hi guys,

I have a recuring method which shows up many times in my code its basically checking to make sure that the connection to the odbc is ok and then connects but each time this method is called it calls another method and each instance of the main method this one is different, as each method is about 8 lines of code having it 8 times in the code isnt ideal.

so basically i would like to have just one method which i can call passing the name of the new method as an arguement.

so basically like:

private void doSomething(methodToBeCalled) { if(somthingistrue) { methodToBeCalled(someArgument) } }

is this possible?

thanks in advance

+2  A: 

You can use delegates, this is much like a pointer to a function, you can pass a delegate to the method, which will invoke it with the parameter.

public delegate void Del(string message);
// Create a method for a delegate.
public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}
// Instantiate the delegate.
Del handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

In your case

private void doSomething(Del methodToBeCalled) 
{ 
    if(somthingistrue) 
    { 
        methodToBeCalled(someArgument) 
    } 
}

is this possible?

Delegates

Svetlozar Angelov
I would suggest to use the `Action<T>` delegate found in the BCL rather than defining custom delegate types. Less stuff to maintain, if nothing else.
Fredrik Mörk
Yes, this is a good point, use everything that is already done in the framework, rather than the custom implementations, much like throwing the exceptions.. a lot of people just love to create custom exceptions
Svetlozar Angelov
A: 

use Delegates as your method argument and you can point the delegate to whichever method you want, providing you stick to your delegate signature.

Tony
A: 

You could also use reflection. Which one is better to use (reflection vs. delegates) depends on the rest of your code. If you're always calling methods that take the same parameters, then a delegate is probably most appropriate. If you need to call methods that take different parameters, then you probably have to use reflection. Looking at your question, it kind of looks like your methods take no parameters, so I'd use delegates as mentioned before.

Aerik
You don't necessarily need reflection to call a function with arguments. You can work around this limitation e.g. with lambda functions -- see my answer for an example.
stakx
+5  A: 

As already said, you can use delegates for this:

// as in the original post:
private void doSomething(Action methodToBeCalled)
{
    if (somethingIsTrue)
    {
        methodToBeCalled();
    }
}

For methods without any arguments, this method is called e.g. as follows:

private void someMethod()
{
    // ...
}

doSomething(someMethod);

If you want to call a method with arguments, you can wrap a lambda function around it:

private void someMethodWithArgument(int arg)
{
    // ...
}

doSomething( () => someMethodWithArgument(42) );

Of course, if your methods to be called always take the same kind of argument, you can declare your doSomething method so that it accepts an Action<T> / Action<T,T> / etc. argument instead. If you want the called methods to return a value, use a delegate from the Func<T> family instead.

stakx