7

The values are comes from xml, user only declare what condition to do.

string condition ="25<10";

Now, I want to use this in if condition like:

if(condition)
{
    //my condition
}

and I am getting this error

Cannot implicitly convert type string to bool

Can anyone tell me how to do this?

Vinoth
  • 972
  • 1
  • 16
  • 47
Vinoth
  • 83
  • 1
  • 1
  • 6
  • 1
    https://ncalc.codeplex.com/ – L.B Sep 12 '17 at 07:24
  • Certainly you cannot convert string to bool directly (if-condition requires bool value). You need to use numeric comparison operator to do so (`if (25 < 10)` is valid, but `if ("25 < 10")` is not valid). – Tetsuya Yamamoto Sep 12 '17 at 07:25
  • 3
    Why didn't the user specify "false" or "no" instead? I suspect your actual expressions are much more complex and a valid answer would depend on how complex. See the comment by @L.B for one way, but it might not be enough or even correct depending on the actual syntax. To be blunt, if you decided on the syntax before figuring out how to treat the expressions you have started in the wrong direction, you may find that you have allowed syntax that you have to implement support for yourself. – Lasse V. Karlsen Sep 12 '17 at 07:26
  • One of the fines example of Expression Builder is [Kendar Expression builder](http://www.kendar.org/?p=/dotnet/expressionsbuilder) where you can create Conditions which are particular operation with return type of bool. – Mohit S Sep 12 '17 at 07:29
  • I think [this question](https://stackoverflow.com/questions/821365/how-to-convert-a-string-to-its-equivalent-linq-expression-tree) deals with exactly your problem, changing a string to an expression tree. – oerkelens Sep 12 '17 at 07:30
  • I agree, that this question is far too broad to be answered. What about more complex conditions such as `"25 + 10 / 3 >= 10 * 3"` or even something like `list.Contains(value)`? – MakePeaceGreatAgain Sep 12 '17 at 07:31
  • Vinoth, depending on the syntax and how compex it is, you may also try `var res = new DataTable().Compute("25 + 10 / 3 >= 10 * 3", "");` – L.B Sep 12 '17 at 08:04
  • Possible duplicate of [Check if condition in string format in c#](https://stackoverflow.com/questions/32516782/check-if-condition-in-string-format-in-c-sharp) – ASh Sep 12 '17 at 08:06

3 Answers3

8

If provided conditions are not that complex, you can try an old trick with 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
  • 180,369
  • 20
  • 160
  • 215
0

First of all: If you do this string condition ="25<10"condition will have the value 25<10 and not true or flase! If 25, 10 and the < come from your xml paste them into 3 different string like x, y and z and compare them like:

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
  }

Maybe there is a better way to transform a pure string input into a if clause but this is the way i would go.

T. Jung
  • 3,327
  • 3
  • 11
  • 19
  • Suppose OP has combined expression in single string like `x = "25 < 10"`. This requires `Contains` method to determine less-than operator & `String.Split` to get 2 operands, then you can compare them in if-condition. – Tetsuya Yamamoto Sep 12 '17 at 07:42
  • Yes your right. I'll correct this in a few seconds. – T. Jung Sep 12 '17 at 07:45
  • Although the answer may answer the question it´s quite specific to it and thus probably not of a great use on StackOverflow except from the OP. – MakePeaceGreatAgain Sep 12 '17 at 07:48
  • Hi T.Jung, is there any way to use without if condition, because if we didn't mention any condition in IF means, the condition won't work so that I am asking. – Vinoth Sep 12 '17 at 08:01
  • @Vinoth - What does "if we didn't mention any condition in IF means, the condition won't work so that I am asking" mean? – Enigmativity Sep 12 '17 at 08:23
0

You can use this code for do it:

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
}

If you just want check leftOperand < rightOperand you can use ternary operator like this:

bool condition = Math.Sign(leftOperand.CompareTo(rightOperand)) == -1 ? true : false;
Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • Hi Ali, is there any way to use without if condition, because if we didn't write all the condition in IF means, user won't get result so that i am asking – Vinoth Sep 12 '17 at 08:02
  • you can use something like this `condition ? first_expression : second_expression;` i guess but it's still an if condition just in the short version – T. Jung Sep 12 '17 at 08:05
  • @Vinoth Do you just want to check if it is bigger (`leftOperand < rightOperand`)? – Ali Soltani Sep 12 '17 at 08:07
  • @Vinoth I added code that is for check `leftOperand < rightOperand` without `if`. Please see this. – Ali Soltani Sep 12 '17 at 08:17