tags:

views:

74

answers:

4

Hello, lets say I have this code here:

if(Something)
    condition |= Class1.Class2.SomeId;
else
    condition &= Class1.Class2.SomeId; 

The code is the same except for the &= and |=. Can I somehow create 2 operator "variables" and just use them in the code, like this:

condition "myoperator" Class1.Class2.SomeId;

Thanks :-)

+2  A: 

No. You could make a function for each, though.

if (Something)
    idOr(ref condition, Class1.Class2.SomeId);
else
    idAnd(ref condition, Class1.Class2.SomeId);


function idOr(ref condition, whatever ID) {
    condition |= ID;
}
function idAnd(ref condition, whatever ID) {
    condition &= ID;
}
Fosco
Yes, I was just wondering if thats possible somehow. So the operator keyword is used for overloading operators only?
grady
How about a lambda?
Mau
You can find more on http://msdn.microsoft.com/en-us/library/8edha89s(v=VS.80).aspx?ppud=4.
ghaxx
I dont get what u try to tell me :)
grady
If you could create new operators in code, it would make parsing the code significantly more complicated and potentially ambiguous.
bruceboughton
I added example code for this, which to me makes much more sense than using a Lambda. Also with the Lambda example, how can you be sure that "Something" is in scope if it is not passed?
Fosco
The problem with the functional approach is that you have to do an `if-else` check before each time you want the operation performed. If you add a level of indirection (by using lambda expressions) you can the check once and then call the lambda in a single line from then on.
Brian Gideon
@Brian, I think you're just re-locating the if-else check to the Lambda. If the goal is a single line call, the same could be achieved through the functional approach.
Fosco
@Fosco: Yeah, that's exactly right. I guess I had the idea that there was more substance to the question. That is that the same operation would be performed over and over again. But you're right, you could always do the `test ? when-true : when-false` ternary operator with the functional approach. And honestly that is probably more efficient than the lamda approach since the lambda has to be invoked through a delegate.
Brian Gideon
+2  A: 

No, you cannot do exactly what you are asking, but lambda expressions could be used to the same end.

Func<int, int, int> op;
if (Something)
{
  op = (x, y) => x | y;
}
else
{
  op = (x, y) => x & y;
}

condition = op(condition, Class1.Class2.SomeId);
Brian Gideon
A: 

You can overload only operators of you own classes (not of Boolean)

if condition or SomeId is of your class, you can overload any operator that is on that msdn page (you cannot create entirely new operators).

Kikaimaru
What makes you think SomeId is a boolean ? I think it's an int (or any other integral type)
Thomas Levesque
A: 

You could dynamically generate a function to implement the operator you need :

public Func<T, T, T> GetOperator<T>(ExpressionType exprType)
{
    var p1 = Expression.Parameter(typeof(T), "p1");
    var p2 = Expression.Parameter(typeof(T), "p2");
    var expr = Expression.MakeBinary(exprType, p1, p2);
    var lambda = Expression.Lambda<Func<T, T, T>>(expr, p1, p2);
    return lambda.Compile();
}

...

var op = GetOperator<int>(Something ? ExpressionType.Or : ExpressionType.And);
condition = op(condition, Class1.Class2.SomeId);

But that's probably overkill... if you don't need a generic solution, just use Brian's solution.

Thomas Levesque