Home > Back-end >  Pass Task as a Parameter in C# get me cannot convert from 'method group' to 'Task
Pass Task as a Parameter in C# get me cannot convert from 'method group' to 'Task

Time:09-23

I have this method that create splash screen

public static async Task CreateSplashScreen(Task action, Form form, string description)
{
    var splashScreenManager = new SplashScreenManager(form, typeof(WaitForm1), true, true)
    {
        SplashFormStartPosition = SplashFormStartPosition.CenterScreen,
    };
    splashScreenManager.ShowWaitForm();
    splashScreenManager.SetWaitFormDescription(description);
    await action;
    splashScreenManager.CloseWaitForm();
    splashScreenManager.Dispose();
}

And here's a method for creating and previewing a report

public static async Task CreateBlackProductionProjectReport()
    {
        using RepCharpenteProductionByShiftTime report = new();
        SmartHelpers.AddDateRangeShiftTimeQueryParameter(report.sqlDataSource1.Queries[0].Parameters);
        SmartHelpers.AddDateRangeParameter(report);
        await SmartHelpers.AddShiftTimeParameter(report);
        report.ShowRibbonPreviewDialog();
    } 

On click of a button I call CreateSplashScreen method like that

private async void ShowBlackProductionProjectReport(object sender, ItemClickEventArgs e)
{
    await CreateSplashScreen(CreateBlackProductionProjectReport,
                                             this, 
                                             ReportDescription);
}

I got this error

cannot convert from 'method group' to 'Task'

I try with Action instead of Task as a parameter

public static void CreateSplashScreen(Action action,Form form, string description)

but I got

'Task CreateBlackProductionProjectReport()' has the wrong return type

What am I missing and how do I fix it?

CodePudding user response:

Your first try almost got it correct, you just missed a pair of parenthesis :

For the following signature :

public static async Task CreateSplashScreen(Task action, Form form, string description)

The call would look like :

private async void ShowBlackProductionProjectReport(object sender, ItemClickEventArgs e)
{
    await CreateSplashScreen(CreateBlackProductionProjectReport(),
                                             this, 
                                             ReportDescription);
}

Without them you are passing what could be assimilated to a Func<Task>

  • Related