我一直面临一个非常基本的问题,我想验证文本框不等于零( != "0“)验证。但是,即使文本框为零,我用来验证的if条件也变成了true并允许。
textbox.text = "0";
if(textbox.text != "0")
{
//textbox value is zero but, if statement becomes true somehow and execute the code inside the if statement that shouldn't be happen right?. I need to know why.
}但是,如果我验证为等于零( == "0“)工作
if(textbox.text == "0")
{
//do something
}
else
{
//now the condition worked and comes to else part.
}验证null或空也会发生这种情况,我知道我们可以使用string.IsnullorEmpty来验证字符串或null。但是,我想知道为什么即使textbox值为空或空或为零,if语句不等于也不起作用。
发布于 2022-04-09 09:33:44
“文本框为零”,您需要了解=和==之间的区别。
textbox.text == "0"检查.text属性的值是否为零,并返回true或false。
textbox.text = 0将.text属性值设置为零。
而不是:
textbox.text=="0";
if(textbox.text!="0")
{
//do something.
}做:
textbox.text="0";
if(textbox.text!="0")
{
//do something.
}https://stackoverflow.com/questions/71806750
复制相似问题