Ahmet Altun
Ahmet Altun

Reputation: 4039

Expression Creator Program - C#

I need to write a program in C# that facilitates the input of logical expression.

There is an operator set which includes >, <, >=, <=, =, != There are AND, OR and parenthesis.

There is going to be a menu that user can select operators and input the values to compare so that the output will be something like:

(A > 5) OR (B = 10 AND C != 50)

How would you suggest to do it in a way that user always has to input valid values. Do you know any articles about it?

Upvotes: 0

Views: 170

Answers (6)

Morten
Morten

Reputation: 3854

Maybe Operator overloads are what you look for???

Something like:

internal class Person
{
    public int Age { get; set; }

    public static bool operator >(Person thisPerson, int compareValue)
    {
        return thisPerson.Age > compareValue;
    }

    public static bool operator <(Person thisPerson, int compareValue)
    {
        return thisPerson.Age < compareValue;
    }

}

Upvotes: 0

weismat
weismat

Reputation: 7411

I have used the Windows Workflow Activities Rule engine in the past. It gives you an Excel/Access style GUI to define rules based on objects, but might offer a lot more than you need.
Have a look here as a start.

Upvotes: 0

Dave White
Dave White

Reputation: 3461

It sounds like what you are trying to do is build an application that will allow users to build Expression Trees (within some constraints).

Here is a really good article by Charlie Calvert describing what expression trees are, how you might use them and how to build them.

I hope this gets you going in the right direction.

Upvotes: 1

Brandon Moretz
Brandon Moretz

Reputation: 7621

Perhaps you should look at implementing a RDC.

http://en.wikipedia.org/wiki/Recursive_descent_parser

There are tons of articles on the web, just google.

Upvotes: 1

TcKs
TcKs

Reputation: 26632

Maybe the Dynamic LINQ can solve your issue.

Upvotes: 0

Related Questions