user323774
user323774

Reputation: 430

Is there a .NET class that represents operator types?

I would like to do the following:

*OperatorType* o = *OperatorType*.GreaterThan;

int i = 50;

int increment = -1;

int l = 0;

for(i; i o l; i = i + increment)
{
    //code
}

this concept can be kludged in javascript using an eval()... but this idea is to have a loop that can go forward or backward based on values set at runtime.

is this possible?

Thanks

Upvotes: 7

Views: 231

Answers (5)

arena-ru
arena-ru

Reputation: 1020

You can define your own class and implement your own operator, see http://msdn.microsoft.com/en-us/library/aa288467(VS.71).aspx

--edited
Oh, misunderstood what you wanted, the best way for you is to use expression or Func<> as Andrey sad.

Upvotes: 0

Johannes Rudolph
Johannes Rudolph

Reputation: 35761

Yes, it's in .NET Expression trees. Specifically, you need to use BinaryExpression.Add(). Building expression trees doesn't need to be done by hand, the compiler will be happy to convert any lambda expression it sees assigned to Expression<T> into a valid Expression tree.

// Creating an expression tree.
Expression<Func<int, int, bool>> greaterThan = (l, r) => l > r;

int i = 50;

int increment = -1;

int l = 0;

for(i; greaterThan(o, i); i = i + increment)
{
    //code
}

Invoking your expression tree will automatically compile it into a dynamic method and greaterThan will effectively act like a delegate.

Upvotes: 13

Mark Synowiec
Mark Synowiec

Reputation: 5445

        Func<int, int, bool> o = (x, y) => x > y;

        int i = 50;

        int increment = -1;

        int l = 0;

        for(; o(i, l) ; i = i + increment)
        {
            //code
        }

or get rid of l altogether:

        Predicate<int> o = (x) => x > 0;

        int i = 50;

        int increment = -1;

        for(; o(i) ; i = i + increment)
        {
            //code
        }

Upvotes: 1

Alan Jackson
Alan Jackson

Reputation: 6511

Edit: Added one with lambda function:

    Func<int, int, bool> lessThan = (num1, num2) => num1 < num2;
    Func<int, int, bool> greaterThan = (num1, num2) => num1 > num2;
    public void Run()
    {
        int increment = -1;
        int l = 0;
        Func<int, int, bool> c = lessThan;
        for (int i = 50; c(i, l); i = i + increment)
        {

        }
    }

I'm sure people will come up with much more elegant solutions than this, but here it is:

    public enum Comparison
    {
        GreaterThan,
        LessThan
    }
    public bool Compare(int a, Comparison c, int b)
    {
        if (c == Comparison.GreaterThan)
            return a > b;
        else if (c == Comparison.LessThan)
            return a < b;
        else
            throw new ArgumentException();
    }

    public void Run()
    {
        int i = 50;
        int increment = -1;
        int l = 0;
        Comparison c = Comparison.GreaterThan;
        for (i; Compare(i, c, l); i = i + increment)
        {

        }
    }

Upvotes: 2

Andrey
Andrey

Reputation: 60105

Func<int,int,bool> op = (i1, i2) => i1 > i2;

then

op(i, l);

Upvotes: 4

Related Questions