0

Hello great community :)

I'm implementing my own network wrapper here and have few questions:

1)

The idea: I want to do static "Network" class and refer to it as for example:

public class NetworkTest
{
    public void StartPoint()
    {
        Network.LogIn(login, pass, OnLoginResponse);
    }

    public void onLoginResponse(ResponseObject obj)
    {
        //deal with response
    }
}

public static class Network
{
    private delegate ResponseObject RequestDelegate(string login, string pass, Action<ResponseObject> obj);

    public void LogIn(string login, string pass, Action<ResponseObject> callBack)
    {
        RequestDelegate request = new RequestDelegate(new WebCommunicator().PostRequest);
        request.BeginInvoke(login, pass, callBack, new AsyncCallback(AsyncResponse), null);
    }

    private static void AsyncResponse(IAsyncResult result)
    {
        AsyncResult res = (AsyncResult)result;
        RequestDelegate request = (RequestDelegate)res.AsyncDelegate;
        ResponseObject response = request.EndInvoke(result);

        if(response != null)
        {
            response.CallBackMethod.Invoke(response);
        }
    }
}

but what if my "NetworkTest" implemented from template interface where i know for 100% that i would have methods like:

OnLoginResponse(ResponseObject obj)
OnRegisterResponse(ResponseObject obj)

and so on....

how can I delegate to them without passing them as argument to:

Network.LogIn(login, pass, OnLoginResponse);

so it looks like:

public class NetworkTest : INetInterface
{
    public void Starter()
    {
        Network.LogIn(login, pass);
    }

    //implemented trough interface
    public void OnLoginResponse(ResponseObject obj)
    {
        //handle response
    }
}

public static class Network
{
    private delegate ResponseObject RequestDelegate(string login, string pass);

    public void LogIn(string login, string pass)
    {
        RequestDelegate request = new RequestDelegate(new WebCommunicator().PostRequest);
        request.BeginInvoke(login, pass, new AsyncCallback(AsyncResponse), null);
    }

    private static void AsyncResponse(IAsyncResult result)
    {
        AsyncResult res = (AsyncResult)result;
        RequestDelegate request = (RequestDelegate)res.AsyncDelegate;
        ResponseObject response = request.EndInvoke(result);

        if(response != null)
        {
            //how to fire NetworkTest.OnLoginREsponse(response) over here?
        }
    }
}

but call back still fall into OnLoginResponse as it is 100% because class made of pre-defained interface and such method is 100% there?

---------------------------------------------------------------------------------------

2)

Also 2nd question, how can I verifay in Network class that whoever called LogIn method was created from specific interface?

For example if I would forget to create my "NetworkTest" from template interface and would call Network.LogIn()
then "Network" calss would answer me that:
"you cant call LogIn() because you are not made of pre-defained interface, thu doesnt contain desired call back methods"


Thank you very much in advance :)

Bryksin
  • 2,322
  • 3
  • 19
  • 31
  • Answer to first question: `public abstract void onLoginResponse(ResponseObject obj);` without a body ensures that inheriting classes implement it. – Silvermind Feb 28 '14 at 12:11
  • Hello, thank you for responding, but im not sure I understand your answer. My 1st question was how to delegate to method without passing it as parram if I know that method 100% is there. Your answer would answer on: how to declare method in interface. here is more detailed explanation: if I do: Network.LogIn(login, pass, OnLoginResponse); then when I got response from login ser I could take 3d param "OnLoginResponse" which i pass in as delegate and invoke it to fire event in caller class but how would i invoke it, if I didnt pass it? – Bryksin Feb 28 '14 at 12:32
  • I update main question with more code to be more detaild on what i am asking – Bryksin Feb 28 '14 at 12:48

0 Answers0