C#bool方法显示错误

问题描述 投票:-4回答:2
public bool Example_bool (bool state)
{
    if (x == state)
    {
        button.Enabled = false;
    }
    else
    {
        button.Enabled = true;
    }
}

为什么会出现错误?我想在某个条件运行时停用按钮,如果没有则应该启用按钮。

c# user-interface
2个回答
4
投票

你有一个方法,希望返回一个bool,这个方法不返回任何东西。如果用bool替换void,错误就会消失。

public void Example_bool (bool state)
{
    if (x == state)
    {
        button.Enabled = false;
    }
    else
    {
        button.Enabled = true;
    }
}

或者更紧凑:

public void Example_bool (bool state)
{
    button.Enabled = x != state;
}

-3
投票

看看你的陈述。 1.什么是x在哪里定义?既然你要通过布尔,为什么不这样简化呢?

public void Example_bool (bool state)
{

        button.Enabled = state;

}
© www.soinside.com 2019 - 2024. All rights reserved.