tags:

views:

161

answers:

5

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

+13  A: 

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.

Johannes Rudolph
Whats the reason to use Expression<T> instead of just the plain Func delegate?
fearofawhackplanet
Using func would simply create an anonymous function bound to a delegate. It will be constructed at compile time. Assigning the lambda to Expression on the other hand causes the compiler to emit an expression tree that can be manipulated at runtime. The anonymous method cannot.
Johannes Rudolph
+4  A: 
Func<int,int,bool> op = (i1, i2) => i1 > i2;

then

op(i, l);
Andrey
+2  A: 

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)
        {

        }
    }
Alan Jackson
+1  A: 
        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
        }
Mark Synowiec
A: 

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.

mmcteam.com.ua