无法转换方法组.不缺少括号

本文关键字:转换方法 | 更新日期: 2023-09-27 18:25:04

bool connected = false;    
if (isConnected())  //if(isConnected() == true) also doesn't work
        {
          //code
        }
        else {
           connect();
        }
public bool isConnected() {
    if (nextEvent != "null" && !nextEvent.Contains(getEvent("disconnected"))) {
        connected = true;
    }
    return connected;
}

收到错误:

无法转换方法组"isConnected"到非委托类型"bool"。

为什么?我查过这个,在大多数情况下,人们忘记在函数名称后加上括号,如下所示:

if(isConnected) { // .... }

对我来说不是这样。怎么了?

无法转换方法组.不缺少括号

您可能

正在尝试在其他函数中定义函数,如果是这样,请将函数放在外部函数中,假设用于调用的代码在YourFun()内部,然后isConnected()定义外侧YourFun()

void YourFun()
{
    bool connected = false;    
    if (isConnected())  //if(isConnected() == true) also doesn't work
    {
          //code
    }
    else {
           connect();
    }
}
public bool isConnected() {
    if (nextEvent != "null" && !nextEvent.Contains(getEvent("disconnected"))) {
        connected = true;
    }
    return connected;
}