1

I am generating a expression from some business rules and it might look like this

0 > 1
12 < 14
"abc" != "xyz"
90 >= 12

Now I have to do certain implementations based on that condition. For example:

 string condition = "0 =1";
 if(condition)
 {
  // do something because condition is passed
 }
else
 { 
  // do something because condition is failed
 }

I have tried to do the same with the dynamic keyword but it is still not working. Any work around?

Edit : 1 modified code

string _initExp = "1";
string _validateCondition = "== 0";
string strcondition = _initExp + _validateCondition;
bool _condition = Convert.ToBoolean(strcondition); // Error statement

if (_condition)
{

}
4

1 Answer 1

4

Why not just use bool:

bool condition = 0==1;
if(condition)
{
   // do something because condition is passed
}
else
{ 
   // do something because condition is failed
}

You can also simplify the following code:

bool condition = 0==1;
if(condition)
{
   return true;
}
else
{ 
   return false;
}

to:

bool condition = 0==1;
return condition;

Or for custom return values

bool condition = 0==1;
return condition ? "yes" : "no";
Sign up to request clarification or add additional context in comments.

8 Comments

return condition; would be sufficient. Why to check once again.
Or you could even use a lambda expression of some sorts.
@Bharadwaj, Thank you. I wasn't able to express myself properly. I have updated my answer. Hope it is better.
I am getting my condition in string format ex: "0 == 1" , when I am using below code to convert it to bool it is not working, probably because it is not able to parse this string as bool bool _condition = Convert.ToBoolean(strcondition)
@Zerotoinfinite, I think this will help: stackoverflow.com/questions/5029699/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.