1
votes

I am trying to execute the error handling component class within my program but keep getting the following error message:

Error 1 The type arguments for method 'Marketplace.ErrorHandlingComponent.Invoke(System.Func, int, System.TimeSpan)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I'm not sure what is meant by try specifying the type arguments explicitly.

MainWindow.xaml.cs

    public void SetPlotList(int filterReference)
    {
        // Fill plot list view
        List<PlotComponent.PlotList> plotList = PlotComponent.SelectPlotLists(filterReference);

        // Find the plot list item in the new list
        PlotComponent.PlotList selectPlotList =
            plotList.Find(x => Convert.ToInt32(x.PlotId) == _focusPlotReference);

        Dispatcher.Invoke(
            (() =>
            {
                PlotListView.ItemsSource = plotList;
                if (selectPlotList != null)
                {
                    PlotListView.SelectedItem = selectPlotList;
                }
            }));

        int jobSum = 0;
        int bidSum = 0;
        foreach (PlotComponent.PlotList item in PlotListView.Items)
        {
            jobSum += Convert.ToInt32(item.Jobs);
            bidSum += Convert.ToInt32(item.Bids);
        }

        // Determine job/bid list ratio
        Dispatcher.BeginInvoke(
            new ThreadStart(() => JobBidRatioTextBlock.Text = jobSum + " jobs - " + bidSum + " bids"));
    }

    private void ValidateTextbox()
    {
        if (Regex.IsMatch(FilterTextBox.Text, "[^0-9]") || FilterTextBox.Text == "") return;
        try
        {
            ErrorHandlingComponent.Invoke(() => SetPlotList(Convert.ToInt32(FilterTextBox.Text)), 3, TimeSpan.FromSeconds(1));
        }
        catch
        {
            FilterTextBox.Text = null;
        }
    }

ErrorHandlingComponent.cs

    public static T Invoke<T>(Func<T> func, int tryCount, TimeSpan tryInterval)
    {
        if (tryCount < 1)
        {
            throw new ArgumentOutOfRangeException("tryCount");
        }

        while (true)
        {
            try
            {
                return func();
            }
            catch (Exception ex)
            {
                if (--tryCount > 0)
                {
                    Thread.Sleep(tryInterval);
                    continue;
                }
                LogError(ex.ToString());
                throw;
            }
        }
    }
2

2 Answers

1
votes

The contents of your lambda is SetPlotList(Convert.ToInt32(FilterTextBox.Text)).

SetPlotList returns void, but Invoke assumes that the lambda provided to it returns some (non-void) type. It can't infer the type that the lambda returns because it doesn't return a type. SetPlotList returned something, then that Invoke call would function properly.

0
votes

SetPlotList is a void method, but ErrorHandlingComponent.Invoke expects a Func<T> as its first parameter. It needs to invoke the Func and will return the func's return value. Since you're trying to pass it a void method, the compiler complains.