Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

S

Satish

When i compile this function I get the error Cannot convert lambda
expression of type System.delegate because it is not a delegate type.

private void SetStatus(string status)
{
if (this.InvokeRequired)
{
this.Invoke( status => SetStatus(status));
}
else
{
toolStripStatusLabel1.Text = status;
}
}

Is it not possible to use the lambda expression in this case? Do I have
to declare a delegate and use it like the old way?
 
M

Martin Honnen

Satish said:
When i compile this function I get the error Cannot convert lambda
expression of type System.delegate because it is not a delegate type.

private void SetStatus(string status)
{
if (this.InvokeRequired)
{
this.Invoke( status => SetStatus(status));

What type is 'this'? How does the signature of 'Invoke' look?
 
S

Satish

The code is on a windows Form so this is System.Windows.Form. My
intention is to update the status bar on the Form from a background thread.
 
M

Marc Gravell

First - both lambdas and anonymous method must be typed; in this case,
MethodInvoker or Action is the easiest...

You'd probably find that this works:

this.Invoke((Action) (() => SetStatus(status)));

But to be honest, it is just as easy to use anon-methods here:

this.Invoke((Action) delegate {SetStatus(status);});
 
M

Marc Gravell

And a third syntax:

this.Invoke((Action<string>)SetStatus, status);

i.e.
treat SetStatus as a void method that accepts a string, and invoke it,
passing /status/ as the arg.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top