Passing a callback function to another class

C#

C# Problem Overview


I'm basically trying to pass a method to another class to be called later, but can't quite figure this out in C# (I'm still too used to Objective-C).

public class Class1{

    private void btn_click(object sender, EventArgs e)
    {
        ServerRequest sr = new ServerRequest();
        sr.DoRequest("myrequest", myCallback);
    }

    public void myCallback(string str)
    {
    }
}

Then later on I want my ServerRequest class to basically fire the callback function, is this not possible? (I'm basically phoning home to a server for a login response to my software)

I haven't been able to find a way to do this with delegates, continuously get errors. Here is the other class:

public class ServerRequest
{
    public void DoRequest(string request, Delegate callback)
    {
        // do stuff....
        callback("asdf");
    }
}

Is this possible in #? In Objective-C this would be simple and I would just do something like

[myObject performSelector(@selector(MyFunctionName)) withObjects:nil];

C# Solutions


Solution 1 - C#

You can pass it as Action<string> - which means it is a method with a single parameter of type string that doesn't return anything (void) :

public void DoRequest(string request, Action<string> callback)
{
    // do stuff....
    callback("asdf");
}

Solution 2 - C#

public class Class1
    {

        private void btn_click(object sender, EventArgs e)
        {
            ServerRequest sr = new ServerRequest();
            sr.Callback += new ServerRequest.CallbackEventHandler(sr_Callback);
            sr.DoRequest("myrequest");
        }

        void sr_Callback(string something)
        {
            
        }

        
    }

    public class ServerRequest
    {
        public delegate void CallbackEventHandler(string something);
        public event CallbackEventHandler Callback;   
        
        public void DoRequest(string request)
        {
            // do stuff....
            if (Callback != null)
                Callback("bla");
        }
    }

Solution 3 - C#

You have to first declare delegate's type because delegates are strongly typed:

public void MyCallbackDelegate( string str );

public void DoRequest(string request, MyCallbackDelegate callback)
{
     // do stuff....
     callback("asdf");
}

Solution 4 - C#

You could use only delegate which is best for callback functions:

public class ServerRequest
{
    public delegate void CallBackFunction(string input);

    public void DoRequest(string request, CallBackFunction callback)
    {
        // do stuff....
        callback(request);
    }
}

and consume this like below:

public class Class1
    {
        private void btn_click(object sender, EventArgs e)
        {
            ServerRequest sr = new ServerRequest();
            var callback = new ServerRequest.CallBackFunction(CallbackFunc);
            sr.DoRequest("myrequest",callback);
        }

        void CallbackFunc(string something)
        {

        }


    }

Solution 5 - C#

Delegate is just the base class so you can't use it like that. You could do something like this though:

public void DoRequest(string request, Action<string> callback)
{
     // do stuff....
     callback("asdf");
}

Solution 6 - C#

You could change your code in this way:

public delegate void CallbackHandler(string str);

public class ServerRequest
{
    public void DoRequest(string request, CallbackHandler callback)
    {
        // do stuff....
        callback("asdf");
    }
}

Solution 7 - C#

What you need is a delegate and a callback. Here is a nice MSDN article that will show you how to use this technique in C#.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeesuView Question on Stackoverflow
Solution 1 - C#BrokenGlassView Answer on Stackoverflow
Solution 2 - C#JeroenView Answer on Stackoverflow
Solution 3 - C#Wiktor ZychlaView Answer on Stackoverflow
Solution 4 - C#NirupamView Answer on Stackoverflow
Solution 5 - C#RobbieView Answer on Stackoverflow
Solution 6 - C#MarcoView Answer on Stackoverflow
Solution 7 - C#MaciejView Answer on Stackoverflow