【问题标题】:how to use if condition with string inside the condition using c# [closed]如何使用 C# 在条件内使用带有字符串的 if 条件 [关闭]
【发布时间】:2018-02-20 13:22:59
【问题描述】:

值来自xml,用户只声明要做什么条件。

string condition ="25<10";

现在,我想在 if 条件下使用它:

if(condition)
{
    //my condition
}

我收到了这个错误

Cannot implicitly convert type string to bool

谁能告诉我怎么做?

【问题讨论】:

  • 当然不能直接将字符串转换为布尔值(if-condition 需要布尔值)。您需要使用数字比较运算符来执行此操作(if (25 &lt; 10) 有效,但 if ("25 &lt; 10") 无效)。
  • 为什么用户没有指定“false”或“no”?我怀疑您的实际表达要复杂得多,有效的答案取决于复杂程度。请参阅@L.B 的评论以了解一种方式,但根据实际语法,它可能不够甚至不正确。坦率地说,如果您在弄清楚如何处理以错误方向开始的表达式之前就决定了语法,您可能会发现您已经允许您必须自己实现支持的语法。
  • 表达式生成器的一个很好的例子是Kendar Expression builder,您可以在其中创建条件,这些条件是返回类型为 bool 的特定操作。
  • 我认为this question 可以解决您的问题,将字符串更改为表达式树。

标签: c# asp.net


【解决方案1】:

首先: 如果您这样做,string condition ="25&lt;10"condition 将具有值 25 而不是 true 或 flase!如果 25、10 和

string input = "25<10"; //input form your xml
int operatorPosition;
//get the position of the operator
if(input.contains("<")){
  operatorPosition = input.IndexOf("<");
}else{
  operatorPosition = input.IndexOf(">");
}
//maybe i messed up some -1 or +1 here but this should work this
string x = input.Substring(0,operatorPosition-1);
string y = input.Substring(operatorPosition+1, input.length-1);
string z = input.charAt(operatorPosition);

//check if it is < or > 
if(z.equals("<"){
  //compare x and y with <
  if(Int32.parse(x) < Int32.parse(y)){
    //do something
  }else{
    //do something
  }
}
//z does not equal < so it have to be > (if you dont have something like = otherwise you need to check this too)
else{
  if(Int32.parse(x) < Int32.parse(y)){
    //do something
  }else{
    //do something
  }

也许有更好的方法将纯字符串输入转换为 if 子句,但这是我会采用的方式。

【讨论】:

  • 假设 OP 在单个字符串中组合了表达式,例如 x = "25 &lt; 10"。这需要Contains方法确定小于运算符&String.Split得到2个操作数,然后你可以在if条件下进行比较。
  • 是的,你的权利。我会在几秒钟内纠正这个问题。
  • 虽然答案可能会回答这个问题,但它非常具体,因此除了 OP 之外,在 StackOverflow 上可能没有多大用处。
  • 嗨 T.Jung,有没有什么方法可以在没有 if 条件的情况下使用,因为如果我们没有在 IF 方法中提及任何条件,那么该条件将不起作用,所以我在问。跨度>
  • @Vinoth - “如果我们没有在 IF 中提及任何条件,则条件将不起作用,所以我要问”是什么意思?
【解决方案2】:

你可以用这个代码来做:

string statment = "10<25"; // Sample statement
string leftOperand = statment.Split('<').First();
string rightOperand = statment.Split('<').Last();
int relationValue = Math.Sign(leftOperand.CompareTo(rightOperand));
if(relationValue == -1)
{
    // leftOperand < rightOperand
}
else if (relationValue == 0)
{
    // leftOperand == rightOperand
}
else if (relationValue == 1)
{
    // leftOperand > rightOperand
}

如果您只想检查leftOperand &lt; rightOperand,您可以像这样使用ternary operator

bool condition = Math.Sign(leftOperand.CompareTo(rightOperand)) == -1 ? true : false;

【讨论】:

  • 嗨,阿里,有没有什么方法可以不使用 if 条件,因为如果我们没有将所有条件都写在 IF 中,用户将不会得到结果,所以我在问
  • 我猜你可以使用类似 condition ? first_expression : second_expression; 的东西,但它仍然是短版本中的 if 条件
  • @Vinoth 你只是想检查它是否更大(leftOperand &lt; rightOperand)?
  • @Vinoth 我添加了用于检查leftOperand &lt; rightOperand 而没有if 的代码。请看这个。
【解决方案3】:

如果提供的条件不是那么复杂,您可以尝试使用DataTable 的旧技巧:

https://msdn.microsoft.com/en-us/library/system.data.datatable.compute(v=vs.110).aspx

private static bool ComputeCondition(string value) {
  using (DataTable dt = new DataTable()) {
    return (bool)(dt.Compute(value, null));
  }
}

...

string condition ="25<10"; 

if (ComputeCondition(condition)) {
   //my condition  
}

【讨论】:

  • 它正在工作,感谢您的建议
  • 嗨,Dmitry Bychenko,如果条件是 1!=10,这个条件不起作用我该怎么办
  • Put &lt;&gt; insted of !=
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
相关资源
最近更新 更多