简体   繁体   中英

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

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM