1

I have following methods for serial communication:

private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  int rxLength = this._serialPort.BytesToRead;
  byte[] rxBuf = new byte[rxLength];
  try
  {
    rxLength = this._serialPort.Read(rxBuf, 0, rxLength);
    this.BeginInvoke((Delegate) (() =>
    {
      this._dataRx.AddBytes(rxBuf, 0, rxLength);
      this.AddDataToAscii(rxBuf, rxLength, true);
    }));
  }
  catch (Exception ex)
  {
  }
}

...and

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Delegate) (() => { AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}

Both of this methods returns following error: Severity Code Description Project File Line Suppression State Error CS1660 Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

how can I modify the source to define missing delegate type?

Thx.

steelbull
  • 131
  • 4
  • 14
  • 1
    Possible duplicate of [Dispatcher.BeginInvoke: Cannot convert lambda to System.Delegate](https://stackoverflow.com/questions/4936459/dispatcher-begininvoke-cannot-convert-lambda-to-system-delegate) – o_weisman Jul 11 '18 at 08:32
  • 1
    @o_weisman - close but that one is for WPF. The question here needs a tag for the GUI being used. – bommelding Jul 11 '18 at 08:38
  • An *anonymous method* requires the `delegate` keyword, not the Delegate class name. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-methods – Hans Passant Jul 11 '18 at 14:46
  • Does this answer your question? [Cannot convert lambda expression to type 'System.Delegate'](https://stackoverflow.com/questions/9549358/cannot-convert-lambda-expression-to-type-system-delegate) – StayOnTarget Jan 22 '21 at 12:50

1 Answers1

2

Action is a Type of Delegate provided by the .NET framework. The Action points to a method with no parameters and does not return a value.

In your case, to make working your code, replace with Action:

private void _serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  this.BeginInvoke((Action) (() => { 
      AddDataToAscii("\nSerial Error : " + e.EventType.ToString() + "\n", true); }));
}
Antoine V
  • 6,998
  • 2
  • 11
  • 34