0

I have common class which is used by both GUI and CommandLine applications

This class is passed a ReportError function reference which acts differently on GUI and CommandLine.

in the GUI:

 public int GUIReportError(String ToLog)
    {
        MessageBox.Show(ToLog);
        return 0;
    }

in the common Class members:

readonly Func<string, int> ReportError;

in the common Class Constructor:

public CommonClass(Func<string, int> ReportErrorFunc)
{
   ReportError=ReportErrorFunc;
}

Up to now, all is simple but I would integrate the [CallerMemberName] attribute to my log function

public int GUIReportError(String ToLog, [CallerMemberName] string CallingFunction="")
        {
            MessageBox.Show(ToLog, CallingFunction);
            return 0;
        }

So I also updated the Func definition:

 readonly Func<string,  string?, int> ReportError;

note udage of ? to tell it's optional parameter

But When I call this ReportError function, I get a compiler error as follows: CS7036: There is no argument given that corresponds to the required formal parameter 'arg2' of Func<string, string?,int>

if anyone already experienced this kind of problem, I would really appreciate.

Julien
  • 1
  • 1
  • 2
    Does this answer your question? [Optional arguments in a generic Func<>](https://stackoverflow.com/questions/28200009/optional-arguments-in-a-generic-func) – Sinatr Aug 03 '20 at 12:46
  • 1
    `string?` does not mean that the parameter is optional. `string?` is short for `Nullable` – Ackdari Aug 03 '20 at 12:51
  • `string?` is the same a `string` because in C# `strings` can be `null`. Note this does not make the parameter optional. – dimitar.d Aug 03 '20 at 12:55
  • @Sinatr, thanks for your quick support. Your second comment makes me think I have to split the problem in two: First I'll make an additional function in my common class which will gather the name of calling function through the [CallerMemberName] attribute and then pass it to the log funciton. – Julien Aug 03 '20 at 13:06

1 Answers1

0

Yes, Solution is here.

The common Class declares a ReportError function which includes [CallerMemberName] as optional parameter.

This function then calls the delegate function passing 2 strings without having to deal with the optionnality.

Julien
  • 1
  • 1