Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# beginner - Callback in the same function

Tags:

c#

I'm pretty sure that is possible (at least in java it is) and I'm C# beginner.

So I have a function which includes a callback (notify some other method that some work is finished).

I don't want to call another function because I'm losing a parameter there (and can't pass parameters in callback functions). How can I do everything in the same function?

What I'm doing now:

public static Tween Play(Tween tweenToPlay) 
{
    return tweenToPlay.Play().OnComplete(RewindCallback);

}

private static void RewindCallback() 
{
    // Execute some code after Tween is completed
}

What I actually want:

public static Tween Play(Tween tweenToPlay) 
{
    return tweenToPlay.Play().OnComplete(/*Create a function that will execute here*/);
}
like image 622
rootpanthera Avatar asked Nov 28 '22 10:11

rootpanthera


2 Answers

Do you mean a lambda expression, like this?

public static Tween Play(Tween tweenToPlay) 
{
    return tweenToPlay
        .Play()
        .OnComplete(() => {
            //  Do stuff
        });
}
like image 76
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Dec 19 '22 02:12

15ee8f99-57ff-4f92-890c-b56153


You just want an anonymous method?

public static Tween Play(Tween tweenToPlay) 
{
    return tweenToPlay.Play().OnComplete(() => 
    {
        //... your code
    });
}
like image 30
Jonesopolis Avatar answered Dec 19 '22 01:12

Jonesopolis