Click here to Skip to main content
13,625,272 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello friends,
I want to compile OR execute string in if condition.I have a string which contains the statement like conditions.

For Example:
string test="2>4";
if(test)
{//do something
}
else
{}

I want run this type of code.

Thanks in advance.
GAURAV MODI

HI JF2015,
Thanks for your reply,but i don't want mathematical parser.it can be string.

like my string may be of : "('abc'=='as')&&(122>34)"
so in our scenario it is wrong.
so our else part will be called.
Posted 28-Mar-12 0:39am
Updated 13-Oct-17 22:49pm
v3
Comments
Andreas Gieriet 28-Mar-12 16:35pm
   
The solution 1 is the best answer one can give. Your reply does not make sense at all...
A string for itself is a sequence of characters. So, what do you mean by "[...] it can be a string.[...]"?

You might ask yourself first what is the difference between:
- string test1 = "2>4"; and can you call if (test1) ...?
- bool test2 = 2>4 ; and can you call if (test2) ...?
gaurav modi 30-Mar-12 7:01am
   
Hi frnd,can you explain me with a sample code.because my condition contains the too many inner condition.Here my string like :
"( [abc] >=3 ) || ( [xyz] == 3 || [xys] == 5 ) ".
ok?
AND my "[abc]" and "[xyz]" words will be replace by any number or string.so i want to parse that newly string in IF condition.
so i can execute my next line of code based of that IF condition's Result. Could you please help me for this subject?
Andreas Gieriet 30-Mar-12 7:44am
   
What is your understanding of parsing? Any experience?
First of all I have to understand what your expectations are:
- Is the expression syntax C# like or SQL like or completely self-invented?
- Where do the [name] come from? Is this a local varialbe, or is it something else?
- How complex may the [name] be? E.g. inst.part.section.para[5].sent[3].word[2]
- What is the type system? All double only, or any other types allowed too? What about type compatibility? E.g. can I compare double with bool?

What exactly do you want? If it shall be "as if I entered it in C#", then you must go for CodeDomProvider approach.
Andreas Gieriet 30-Mar-12 7:29am
   
Why do you have a string and not the expression directly in C#?
Where do you get your expression from? From a GUI?

If the expression is given in a text (string), then please take a look at the Soultions below or any other Google: C# expression parser. Sample code is from 30 lines up to several hundred. I'll not post them here. Please lookup the internet.

Finally: is this a homework assignment?
gaurav modi 3-Apr-12 8:57am
   
hi thanks for Ur reply,
there is comparison between string to string and int to int.but one string contains both type validation.
suppose my string look like this :
string s = "(obj.statename !=\"TEST\" ) && (obj.cityname !=\"AZ\")";
ok?
AND obj.statename and obj.cityname have value at runtime.
obj is my class and statename and cityname are properties of that class.

so i want just know that given string's condition is true or false.
Andreas Gieriet 3-Apr-12 9:22am
   
See my solution #4 below.
Cheers
Andi
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I have several articles on value converters for evaluating expressions. they should be helpful, and they point to other options. I generally used the Javascript eval function. There is also an article on using Roslyn.

Value Converter to Evaluate User Equation Input[^]

Using Roslyn ScriptEngine for a ValueConverter to process user input[^]

These should help
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

You may use Invent your own Dynamic LINQ parser[^] and leave the obj. in your expression away.

Usage:
public class MyClass
{
   string StateName { get; private set; }
   string CityName  { get; private set; }
   ...
}
...
string expr = "StateName != \"Test\" && CityName != \"AZ\"";
var f = SimpleExpression.PredicateParser<MyClass>.Parse(expr).Compile();
var query = from e in myCollection where f(e) select e;
...
foreach(var item in query)
{
   ...
}


Cheers
Andi
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180712.1 | Last Updated 14 Oct 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100