1

I have a function:

private static Response WaitForAPIReply(Func<Response> f)
{
  while (true)
  {
    var response = f();
    if (response.MISSING == "WAIT")
    {
      Console.WriteLine("WAIT");
      Thread.Sleep(TimeSpan.FromSeconds(0.1));
    }
    else
    {
      return response;
    }
  }
}

And I call WaitForAPIReply as:

private static Response GetTestData(int a, string b)
{
  return WaitForAPIReply(() => GetData(a, b));
}

I want to add one out parameter in GetData and use this in WaitForAPIReply function. But I am unable to modify WaitForAPIReply for this?
I searched over web could see I can use delegate. But I am unable to fix this while passing as parameter to WaitForAPIReply

Nipun
  • 2,217
  • 5
  • 23
  • 38
  • So you want `WaitForAPIReply` to have an `out` parameter as well? Then you want to call it like `WaitForAPIReply((out YourType o) => GetData(a, b, out o), out c);`, or what? It is not clear what your problem is? Maybe then you want to declare it like `private static Response WaitForAPIReply(YourOutFunc f, out YourType yt) { ... }`. Please indicate where your problem is. – Jeppe Stig Nielsen Aug 20 '15 at 13:37
  • @Jeppe, problem is I want to use data received in 'out' parameter of 'GetData' in 'WaitForAPIReply' function – Nipun Aug 21 '15 at 04:15
  • Try what I wrote in my first comment. You must define your own "func" delegate type. It goes something like this: `namespace YourSpace { delegate TResult YourOutFunc(out TOut o); }` which you can put in a new file, `YourOutFunc.cs`. Good luck. – Jeppe Stig Nielsen Aug 21 '15 at 07:29

1 Answers1

1

Why do you want to modify WaitForAPIReply? Notice that right now GetData has two parameters, but they are not used anywhere in your f() call inside WaitForAPIReply. The out parameter should be declared somewhere inside GetTestData, so that you can pass it into GetData.

// First approach
private static Response GetTestData(int a, string b)
{
    int c;
    return WaitForAPIReply(() => GetData(a, b, out c));
}

// Second approach
private static Response GetTestData(int a, string b, int c)
{
    return WaitForAPIReply(() => GetData(a, b, out c));
}
Kapol
  • 6,383
  • 3
  • 21
  • 46
  • The second approach is confusing, and the third one doesn't compile (fortunately - there's no guarantee when and where the lambda will execute, if at all; that's not something you want with an `out` or `ref` argument). – Luaan Aug 20 '15 at 12:38
  • @kapol, your solution is quiet helpful for declaring out parameter. But I want to use 'out c' in 'WaitForAPIReply' function. I missed to mention this in my question, so I am editing my query. – Nipun Aug 20 '15 at 12:43
  • @Luaan Good point about the third approach. I will remove it from the answer. – Kapol Aug 20 '15 at 12:48