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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Satish

    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(strin g status)
    {
    if (this.InvokeReq uired)
    {
    this.Invoke( status =SetStatus(stat us));
    }
    else
    {
    toolStripStatus Label1.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?
  • Martin Honnen

    #2
    Re: Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

    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(strin g status)
    {
    if (this.InvokeReq uired)
    {
    this.Invoke( status =SetStatus(stat us));
    What type is 'this'? How does the signature of 'Invoke' look?

    --

    Martin Honnen --- MVP XML

    Comment

    • Satish

      #3
      Re: Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

      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(strin g status)
      >{
      > if (this.InvokeReq uired)
      > {
      > this.Invoke( status =SetStatus(stat us));
      >
      What type is 'this'? How does the signature of 'Invoke' look?
      >

      Comment

      • Marc Gravell

        #4
        Re: Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

        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((Ac tion) (() =SetStatus(stat us)));

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

        this.Invoke((Ac tion) delegate {SetStatus(stat us);});

        Comment

        • Marc Gravell

          #5
          Re: Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

          And a third syntax:

          this.Invoke((Ac tion<string>)Se tStatus, status);

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

          Marc

          Comment

          • Satish

            #6
            Re: Cannot convert lambda expression of type System.delegate becauseit is not a delegate type

            Thanks it worked!!

            Marc Gravell wrote:
            And a third syntax:
            >
            this.Invoke((Ac tion<string>)Se tStatus, status);
            >
            i.e.
            treat SetStatus as a void method that accepts a string, and invoke it,
            passing /status/ as the arg.
            >
            Marc

            Comment

            Working...