Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a value like "1=1","2>1","5!=6" in string variable.i want check this value,is true or false.

Ex:
C#
string A="2>1";
string B="5!=6";

my result should be

if(A)
{
   return true;
}

if(B)
{
   return false;
}
Posted
Comments
Dholakiya Ankit 16-Jul-13 8:27am    
get one function like this
function (value a ,value b,value c)
{
if(a + b + c)
{
return true;
}
else
{
return false;

}
}

1 solution

It's not so simple as you might think if you want to do it right. Basically you need to compile or parse the text to evaluate it. Here's a link that might be of help, though:

http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx[^]

There are also quite a few articles about it here on The CodeProject:

The expression evaluator revisited (Eval function in 100% managed .NET)[^]
Evaluation Engine[^]
State of the Art Expression Evaluation[^]
http://www.codeproject.com/Articles/241830/A-Tiny-Expression-Evaluator[^]

Good luck.

A simpler approach would be to build a parser of your own that can parse the expressions and calculate the outcome, but that can be complicated as well if you need to take a lot of different scenarios into consideration.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900