【发布时间】:2013-07-25 10:38:50
【问题描述】:
我有这个代码:
if (textBox1.Text == "one" || "two")
我尝试过使用 ||和 |添加更多字符串,但它说它不能应用于“bool”和“string”类型的操作数。我怎样才能使这项工作? 谢谢。
【问题讨论】:
-
有哪些不同的情况?
我有这个代码:
if (textBox1.Text == "one" || "two")
我尝试过使用 ||和 |添加更多字符串,但它说它不能应用于“bool”和“string”类型的操作数。我怎样才能使这项工作? 谢谢。
【问题讨论】:
试试这个
if (textBox1.Text == "one" || textBox1.Text == "two")
【讨论】:
textBox1.Text == "one" || "two"。你不能这样做 - 左边的表达式将被评估为布尔值,然后你试图评估 boolean || "two",导致你提到的错误。
或者:
var strings = new List<string>() {"one", "two", "thee", .... "n"};
if(strings.Contains(textBox1.Text)){
}
【讨论】:
(c.Contains('All') || c.Contains('')) ? "[ClientText] LIKE '%'" : "[ClientText] = '" + c + "'"; cannot convert string to bool error.
您不能以我怀疑您正在尝试的方式组合运算符:
if (textBox1.Text == "one" || "two")
您需要对每个条件进行如下限定:
if (textBox1.Text == "one" || textBox1.Text == "two")
有一些方法可以让这更容易,请参阅this question 的答案以了解另一种方法
【讨论】:
我建议使用:
var options = new [] { "one", "two" };
if (options.Contain(textBox1.Text))
...
【讨论】: