Sign in to follow this  

[C#] Question about functions

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if it is possible to pass a function as a parameter for a different function, I think this is possible in C++ but i have no idea if you can do it in C#. Basically, what i want to do is this:
class A
{
    public void B(function C)
    {
        C();
    }
}
So basically, B would be a function in a certain class and C would be passed into that class for maybe rendering or updating. Also, would it be possible to make it so I can set B as a new function in class A so i can access it from other functions in the class? Thanks in advance, David

Share this post


Link to post
Share on other sites
This is how you do the second question.


class A
{
public delegate void Function_Call();
public Function_Call function_call;

public void Function_A()
{
System.Windows.Forms.MessageBox.Show("In Function_A");
}

public void Function_B()
{
System.Windows.Forms.MessageBox.Show("In Function_B");
}

public void SetFunction(char function)
{
switch (function)
{
case 'A':
function_call = Function_A;
break;
default:
function_call = Function_B;
break;
}
}

}

A a = new A();
a.SetFunction('A');
a.function_call();






Just have to have the same parameter definition. Well for the most part. We won't get into generic delegates right now.

On a side note and to prevent confusion. In a class they are really called 'methods' not functions. I used the OP terminology just for clarity of what he was asking.

Have to love delegates.
theTroll

Share this post


Link to post
Share on other sites
In .NET 3.5 there are a bunch of predefined delegates that you can use (all defined in System.Core.dll in namespace System):


public delegate void Action();
public delegate void Action<T>(T obj);
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<TResult>();
public delegate TResult Func<T, TResult>(T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);

// So for the original question a solution is:
class A
{
public void B(Action c)
{
C();
}
}



Quote:
Original post by TheTroll
Have to love delegates.
theTroll


Agreed. And lambda syntax in C# 3.0 makes it even cooler:


// Extension method for IEnumerable<TSource> available in .NET 3.5:
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);

var items = new[] { "hello", "gamedev", "chicken", "dog" };
double averageLength = items.Average(x => x.Length);

Share this post


Link to post
Share on other sites

This topic is 3490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this