Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Hello everyone,

I want to evaluate dynamic if statement like the e.g below
int a=2;
String condition="a==2";

if(condition)
execute something
else
execute something

I mean that the condition is changeable, and it's coming from database.
For example:
String condition="";
int a, b, c, d;

....
The condition could be:
condition = "a == 2";

or could be:
condition = "a >= b && c == a && ((d - 4) == a ) ";

or
condition = ".....something else......";


if(condition)
execute something
else
execute something


The condition is not constant. That's my problem.

How i can do this... Please let me know..
Thanks in advance

[Modified: just changed the code tags to pre tags...a bit easier to read that way IMO]
Posted
Updated 20-Oct-10 7:09am
v5

It might be possible using the .NET framework built in code compiler. I have never done this myself, but i have seen demos of it done with IronRuby, etc. Admittedly they were dynamic languages, but should be possible to do it in C#.

This[^] article on CodeProject might help.

There is also a calculator example here[^].

If the user is able to enter the a ==2 etc, you will have to be very careful checking input to make sure they don't do anything nasty. Not sure how you would go about doing this.
 
Share this answer
 
You need to write an expression evaluator that can take a string, parse it, and evaluate a mathematical expression with variables. There's nothing built-in that can do this, so you need to write your own or search for a 3rd party implementation.
 
Share this answer
 
Comments
Ankur\m/ 19-Oct-10 3:40am    
Okay, I missed the question completely. :(
I am not very clear about your question.

What's the problem with the below code (C#):
C#
int a = 2;
if (a == 2)
{
    //execute something
}
else
{
    //execute something else
}

And what do you mean by evaluating dynamic if statement?
 
Share this answer
 
hi ,
your code snap is like this :

int a=2;
String condition="a==2";

if(condition)
execute something
else
execute something
Right?
You have to declare your "condition" variable in "dynamic" keyword instead of "string".
 
Share this answer
 
Comments
Andreas Gieriet 28-Mar-12 16:25pm    
dynamic does not help here. Your understanding of dynamic is wrong.
See C# dynamic keyword. The object declared dynamic tells the runtime environment to search for the member at runtime before executing it. What we have here is a string and we do not call anything on it. Instead, it has to be parsed before executed.
- use the code DOM provider
- use Dynamic LINQ or one of the many hand-crafted variants thereof, e.g. my alterantive tip
- any google: C# expression evaluator that lingers around in the web...
Cheers
Andi

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