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?

8
  • 1
    ncalc.codeplex.com Commented Sep 12, 2017 at 7: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). Commented Sep 12, 2017 at 7: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. Commented Sep 12, 2017 at 7:26
  • One of the fines example of Expression Builder is Kendar Expression builder where you can create Conditions which are particular operation with return type of bool. Commented Sep 12, 2017 at 7:29
  • I think this question deals with exactly your problem, changing a string to an expression tree. Commented Sep 12, 2017 at 7:30

3 Answers 3

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  
}
Sign up to request clarification or add additional context in comments.

3 Comments

it is working thanks for your advice
Hi Dmitry Bychenko if the condition is 1!=10, this condition is not working what should i do
Put <> insted of !=
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.

5 Comments

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.
Yes your right. I'll correct this in a few seconds.
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.
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 - What does "if we didn't mention any condition in IF means, the condition won't work so that I am asking" mean?
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;

4 Comments

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
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
@Vinoth Do you just want to check if it is bigger (leftOperand < rightOperand)?
@Vinoth I added code that is for check leftOperand < rightOperand without if. Please see this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.