-1

I am getting lost in the many answers and examples here (Pass Method as Parameter using C#) about passing functions.

At the moment I have this:

private void AddWeekToHistory(ref XDocument xdoc, MSAHistoryWeek historyWeek)
{
   // Do stuff
    DetectStudentItemDescriptionAndType(studentItem,
                                                        bFirstStudent,
                                                        iClass,
                                                        out string strDesc,
                                                        out string strType);
   // Do stuff
}
private void DetectStudentItemDescriptionAndType(MSAHistoryItemStudent studentItem, bool bFirstStudent, int iClass, out string strDesc, out string strType)
{
    // Do stuff
}

I want to change AddWeekToHistory so that it can be passed a DetectStudentItemDescriptionAndType function. This is because I want to add a second version of that function that will use different logic (same parameters).

Ultimately I want to call AddWeekToHistory(ref xdoc, historyWeek, [name-of-func]);.

I understand from the answers that since I am using a void function that I should use Action. But I was getting lost in teh answers because the method in the original question there was passed a parameter and yet in the examples of running the passed function they did not actually pass parameters.

So rather than confuse an existing question I have asked a new one. What changes are needed for me to support passing DetectStudentItemDescriptionAndType and its variants (same attributes) as a function to AddWeekToHistory?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • What exactly didn't work when you tried the implement the answers in the other question? By `attributes` do you mean `parameters`? – Liam Jan 18 '22 at 10:54
  • Does this answer your question? [Delegates: Predicate vs. Action vs. Func](https://stackoverflow.com/questions/566860/delegates-predicate-vs-action-vs-func) – Liam Jan 18 '22 at 10:54
  • 2
    @Liam It's likely that the `out` parameters will be causing the issue. The `Action` delegates don't support them. – Johnathan Barclay Jan 18 '22 at 10:58

1 Answers1

5

You can't use any of the Action<...> delegates for a method with ref or out parameters. You will need a custom delegate instead:

public delegate void DetectStudentItemDescriptionAndTypeDelegate(MSAHistoryItemStudent studentItem, bool bFirstStudent, int iClass, out string strDesc, out string strType);

private void AddWeekToHistory(ref XDocument xdoc, MSAHistoryWeek historyWeek, DetectStudentItemDescriptionAndTypeDelegate detect)
{
    // Do stuff
    detect(studentItem,
        bFirstStudent,
        iClass,
        out string strDesc,
        out string strType);
   // Do stuff
}

private void DetectStudentItemDescriptionAndType(MSAHistoryItemStudent studentItem, bool bFirstStudent, int iClass, out string strDesc, out string strType)
{
    // Do stuff
}

AddWeekToHistory(xdoc, historyWeek, DetectStudentItemDescriptionAndType);

Delegates - C# Programming Guide

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • 1
    There's currently a proposal to introduce support for `out` and `ref` modifiers on `Func` and `Action`: https://github.com/dotnet/csharplang/issues/5321 – silkfire Jan 18 '22 at 12:01