Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot convert from 'method group' to 'System.Action<byte, byte, bool[*,*]>'

I get an error:
Error CS1503 Argument 1: cannot convert from 'method group' to 'System.Action<byte, byte, bool[*,*]>'

This is my minimal example code which I feel should compile but causes the above mentioned error:

using System;
                    
public class Program {
  void EraseGoodBlocks(bool OverridePreviousTests, ref bool[,] DeviceResults) {
    ForAllDevicesResults(eraser, ref DeviceResults);
    void eraser(byte BoardNo, byte DeviceNo, ref bool[,] device_results) {}
  }
    
  void ForAllDevicesResults(Action<byte, byte, bool[,]> action, ref bool[,] result){
    for (byte Board = 0; Board < 4; Board++){
      for (byte Device = 0; Device < 16; Device++){
        action(Board, Device, result);
      }
    }
  }
}

The code uses delegates and references which complicates matters. Where did I go wrong.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The message is not very clear, but the issue is that you are passing eraser where an Action<byte, byte, bool[,]> is expected. And the third parameter of eraser is a ref bool[,], not a bool[,] as expected.

As proposed by Selvin, you may change the signature of eraser and remove the ref on the third parameter.

If you really need to pass the third parameter of the delegate as a ref bool[,], you will not be able to declare the delegate as an action.

Instead you must declare your own delegate:

delegate void BlockAction(byte BoardNo, byte DeviceNo, ref bool[,] device_results);

And add the ref modifier when calling it:

delegate void BlockAction(byte BoardNo, byte DeviceNo, ref bool[,] device_results);

void EraseGoodBlocks(bool OverridePreviousTests, ref bool[,] DeviceResults)
{
    ForAllDevicesResults(eraser, ref DeviceResults);

    void eraser(byte BoardNo, byte DeviceNo, ref bool[,] device_results)
    {

    }
}

void ForAllDevicesResults(BlockAction action, ref bool[,] result)
{
    for (byte Board = 0; Board < 4; Board++)
    {
        for (byte Device = 0; Device < 16; Device++)
        {
            action(Board, Device, ref result);  // add ref here
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading