C# 将公共bool称为公共void
我有一个问题,我想在一个公共课上做一个公共bool,并称之为void 我想检查一下第二节课的bool是不是真的 例如,在我的第一节课上:C# 将公共bool称为公共void,c#,C#,我有一个问题,我想在一个公共课上做一个公共bool,并称之为void 我想检查一下第二节课的bool是不是真的 例如,在我的第一节课上: public class GetWindow { public string Check { get; set; } public bool checkwindow() { if (Listofwindows.Contains(Check)) return true; else
public class GetWindow
{
public string Check { get; set; }
public bool checkwindow()
{
if (Listofwindows.Contains(Check))
return true;
else
return false;
}
}
第二个:
public Form1()
{ [...]
GetWindow myprogram1 = new GetWindow();
myprogram1.Check = "Kin";
if (myprogram1.checkwindow == true)
{/*Do thing*/}
}
显然,它不起作用,因为myprogram1.checkwindow
它说:
无法将方法组“checkwindow”转换为非委托类型bool
运算符“==”不能应用于“方法组”和“布尔”类型的操作数
因为myprogram1.checkwindow==true
因此,这样做似乎是行不通的,但我不知道如何做得不同!(我需要我的getwindow类。)为了在C#中调用方法,您需要在方法名称后添加括号,即使该方法不需要参数:
if (myprogram1.checkwindow() == true)
{
{/*Do thing*/}
}
此外,要计算布尔值,您不需要与文本true
进行比较。您只需编写:
if (myprogram1.checkwindow())
{
{/*Do thing*/}
}
要在C#中调用方法,需要在方法名称后添加括号,即使该方法不需要参数:
if (myprogram1.checkwindow() == true)
{
{/*Do thing*/}
}
此外,要计算布尔值,您不需要与文本true
进行比较。您只需编写:
if (myprogram1.checkwindow())
{
{/*Do thing*/}
}
我会删除无用的属性,并将一个参数传递给
checkwindow
方法
public class GetWindow
{
public bool checkwindow(string check)
{
// Contains already returns true/false, no need of additional checks
return Listofwindows.Contains(check);
}
}
并称之为
public Form1()
{ [...]
GetWindow myprogram1 = new GetWindow();
if (myprogram1.checkwindow("Kin"))
{/*Do thing*/}
}
我会删除无用的属性,并将一个参数传递给
checkwindow
方法
public class GetWindow
{
public bool checkwindow(string check)
{
// Contains already returns true/false, no need of additional checks
return Listofwindows.Contains(check);
}
}
并称之为
public Form1()
{ [...]
GetWindow myprogram1 = new GetWindow();
if (myprogram1.checkwindow("Kin"))
{/*Do thing*/}
}
优化:
public class GetWindow
{
public string Check { get; set; }
public bool checkwindow()
{
return Listofwindows.Contains(Check);
}
}
和cal it:
GetWindow myprogram1 = new GetWindow();
myprogram1.Check = "Kin";
if (myprogram1.checkwindow())
{
{/*Do thing*/}
}
优化:
public class GetWindow
{
public string Check { get; set; }
public bool checkwindow()
{
return Listofwindows.Contains(Check);
}
}
和cal it:
GetWindow myprogram1 = new GetWindow();
myprogram1.Check = "Kin";
if (myprogram1.checkwindow())
{
{/*Do thing*/}
}
你需要
if(myprogram1.checkwindow()==true)
。注意额外的一对括号。你忘记了括号()
我想:myprogram1.checkwindow()==true
你需要if(myprogram1.checkwindow()==true)
。注意额外的一对括号。你忘记了括号()
我想:myprogram1.checkwindow()==true
当他们说“别忘了括号,我没想到这些括号。”。非常感谢。好的,当他们说“别忘了括号,我没想到这些。谢谢!!