469,086 Members | 1,117 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

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


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?
Jun 27 '08 #1
5 5395
Satish wrote:
>
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?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
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.

Martin Honnen wrote:
Satish wrote:
> 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?
Jun 27 '08 #3
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);});
Jun 27 '08 #4
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
Jun 27 '08 #5
Thanks it worked!!

Marc Gravell wrote:
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
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by =?Utf-8?B?UnV0aCBNYXJr?= | last post: by
20 posts views Thread by raylopez99 | last post: by
reply views Thread by Pavel Minaev | last post: by
16 posts views Thread by rowe_newsgroups | last post: by
6 posts views Thread by Stefan Hoffmann | last post: by
11 posts views Thread by puzzlecracker | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.