Chris G.
Chris G.

Reputation: 25934

C# callback parameter

A bit rusty in C#

I need to pass in a callback to a method:

void InvokeScript(string jsScript, Action<object> resultCallback);

In my class I have created a method to pass in to the method:

        public void callback(System.Action<object> resultCallback)
    { 

    }

Error message 1:

Resco.UI.IJavascriptBridge.InvokeScript(string, System.Action<object>)' has some invalid arguments

Error message 2:

cannot convert from 'method group' to 'System.Action<object>'

Thanks in advance

Upvotes: 3

Views: 8232

Answers (5)

maximpa
maximpa

Reputation: 1988

The signature of the callback should be the following:

void MethodName(object parameter);

You also may use lambda expression even without creating a separate method:

InvokeScript(
    "some string",
    p =>
    {
        // the callback's logic
    });

Upvotes: 0

hattenn
hattenn

Reputation: 4399

Try

Action<object> myCallBack = delegate
{
// do something here
};

    InvokeScript("some string", myCallBack);

The method delegate needs to take an object and not return any value. That's what Action<object> means. You can use the built-in Action delegate as I've shown, or you can create a new method and pass it as delegate:

public void MyMethod(object myParameter)
{
    // do something here.
}

InvokeScript("some string", MyMethod);

Upvotes: 0

Botz3000
Botz3000

Reputation: 39600

Either you create a method matching the signature of the Action<object> delegate, such as

public void someMethod(object parameter) { }

and then pass it,
or you can use a lambda:

InvokeScript("stuff", 
    param => { 
        Blah(param); 
        MoreBlah();
    });

Upvotes: 0

Matthew Watson
Matthew Watson

Reputation: 109547

Your callback should be:

public void callback(object value)

Upvotes: 2

LukeHennerley
LukeHennerley

Reputation: 6434

You need to make the parameter your passing an object.

Upvotes: 0

Related Questions