简体   繁体   English

无法分配给“”,因为它是“方法组”

[英]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: 我是C#和一般编程的新手,由于要在大学任教,我被要求编写计算器程序,但现在遇到了一些麻烦,这是我的代码:

    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. 由于它引起的错误显示为“无法分配给'IsInfinity',因为它是一个'方法组'”,因此我正在努力寻找解决方案,任何帮助将不胜感激。 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. 首先, IsInfinity是一个方法(如果有多个重载,则是一组方法),因此您需要使用一些参数来调用它。 Something like 就像是

IsInfinity(number1)

Second, you are trying to set the method to true, rather than comparing its result to true. 其次,您尝试将方法设置为true,而不是其结果与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.) (因为它已经返回true,所以您无需再次进行比较。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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