Search code examples
c#calculatormethod-group

Cannot assign to ' ' because it is a 'method group'


I am new to C# and programming in general, for my assignment at university I have been asked to program a calculator and I am running into some trouble, here is my code:

    private bool IsInfinity(long result)
    {
        if (result > Int32.MaxValue || result < Int32.MinValue)
        {
            errorFound = true;
            return true;
        }
        else
        {
            return false;
        }
    }

    private double Add(double number1, double number2)
    {
        if (IsInfinity = true)
        {
            errorFound = true;
            return 0;
        }
        else
        {
            double result = number1 + number2;
            result = Math.Round(result, 2);
            return result;
        }
    }

I am having trouble with the line,

    if (IsInfinity = true)

as it is causing an error that reads, "Cannot assign to 'IsInfinity' because it is a 'method group'", I am struggling to find a solution to this and any help would be greatly appreciated. Thanks


Solution

  • Your code has two issues.

    First, IsInfinity is a method (or group of methods, if there are multiple overloads), so you need to invoke it with some parameters. Something like

    IsInfinity(number1)
    

    Second, you are trying to set the method to true, rather than comparing its result to true. What you want is

    if(IsInfinity(number1) == true) 
    

    (notice the two equal signs). Or, more tersely,

    if(IsInfinity(number1))
    

    (since it already returns true, and you don't need to do the comparison again.)