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.
>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
}
}
}