How to convert string to expression

I am having an string "3<2", I would like to use it in a if statement as if(3<2).

How to convert this string into an boolean expression.

Can we convert a string like "a<b" into a boolean expression.

[246 byte] By [MuraliSalem] at [2008-2-7]
# 1

You could use CodeDom - which is used to dynamically build up code (using code) to execute... have a look at the System.CodeDom namespace, in particular the CodeExpressionStatement class.

If you have a look through the documentation (and on the internet for examples) you should be able to put something together.

Hope that helps.

Simon

SimonInce at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 2
Use a regular expression to extract out each side of the equation from the string. Convert the results to integers and compare those results appropriatly. Do something like this:

if (Compare("3<2"))
{
... do something ...
}


~
public bool Compare(string equation)
{
bool result = false;
RegEx rx = new RegEx(@"(\d)([\<\>])(\d)"); // Three match groups.

if (rx.IsMatch(equation))
...
// Work with the matches ("3", "<", "2")
...
// Return the appropriate boolean.
return result;
}

OmegaMan at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...

.NET Development

Site Classified