0

I am java developer new to C# concepts. I am trying to call class function from anonymous inner function (in java terms don't know what it is called in C#).

public void test ()
{
    this.apiManager.send (RequestMethod.GET, "/admin", "", callback1);
}


ApiCallback callback = new ApiCallback () {
    onSuccess = (string response, long responseCode) => {
        Debug.Log (response);
        Debug.Log (responseCode + "");
        test();
    },
    onError = (string exception) => {
        Debug.Log (exception);
    }
};

So on doing this I am getting following error

A field initializer cannot reference the nonstatic field, method, or property "test()"

Here is the implementation of ApiCallback

public class ApiCallback
{
    public delegate void SuccessCreater (string response, long responseCode);

public delegate void ErrorCreater (string error);

public SuccessCreater onSuccess { get; set; }

public ErrorCreater onError { get; set; }

}
Abhishek Patidar
  • 1,557
  • 1
  • 16
  • 25
  • Different scope, if you'd like to do this, you have to assign `static` to your test method. – Christoph K Jun 02 '17 at 13:14
  • It would help to see where `test` is declared and _where_ you instantiate `callback`. But I guess it would show that Christoph is right. – René Vogt Jun 02 '17 at 13:15
  • declare the test method as public static void test() – Curious Jun 02 '17 at 13:16
  • Since your new to C#: consider using the [C# naming conventions](https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx) and you may want to read about `event`s in C#. Your code currently only exposes delegate properties, which does work, but only for a single "subscriber". – René Vogt Jun 02 '17 at 13:18
  • making function static would mean I have make inner function also static, if not then have to make main class singleton. Don't want to do that... is there a different way – Abhishek Patidar Jun 02 '17 at 13:22
  • 1
    Is callback meant to be used in the apiManager.send? If so, you could be up for a stack overflow exception since success would call test again and again until a failure. – Everts Jun 02 '17 at 13:55
  • That's why I have given different callback - callback1 and not callback – Abhishek Patidar Jun 03 '17 at 03:19

1 Answers1

2

You must move the instantiation code to your constructor:

public YourClassNameHere()
{
    callback = new ApiCallback()
     {
         onSuccess = (string response, long responseCode) => {
             Debug.Log(response);
             Debug.Log(responseCode + "");
             test();
         },
         onError = (string exception) => {
             Debug.Log(exception);
         }
     };
}

Alternatively use (to switch from field to property):

    ApiCallback callback => new ApiCallback()
    {
        onSuccess = (string response, long responseCode) => {
           Debug.Log(response);
           Debug.Log(responseCode + "");
           test();
        },
        onError = (string exception) => {
           Debug.Log(exception);
        }
    };

See A field initializer cannot reference the nonstatic field, method, or property for more details.

mjwills
  • 23,389
  • 6
  • 40
  • 63