Assuming this isn't in the middle of some inner weather-prediction loop, you
could do:
opRet = DoRange( valueInQuestion,
0, 10, new delegate(int x) { doThis(x); return someMore(); },
11, 15, new delegate { return doThat(); },
40, 99, new delegate { doSomethingElse(); return 1; }
200, int.MaxValue, new delegate(int x) { return wowThatsBig(x*x); }
);
// UNTESTED
delegate void MyOp( int value);
public int DoRange( params object[] stuff )
{
int value = (int) params[0];
for ( int i = 1; i < stuff.Length; i+=3)
{
int begin = (int) stuff[ 0+i];
int end = (int) stuff[ 1+i];
MyOp fn = (MyOp) stuff[ 2+i];
if ( value >= begin && value <= end)
// note: if a value can be in more than one range, we can do
them all instead of returning
return fn( value);
}
}
If you don't need to return values, it's even simpler.
Of course, you could generisize all the types, and use interfaces or virtual
fn's instead of the anonymous delegates, support items in a list rather than
just the range, binary search if you can flag them in order, etc.
Then again, it's not too much prettier than the IF...
Anyone up for porting CL LOOP to c#? :)
using CL;
CLVar v; Hashtable h = ...;
Loop( FOR, v, BEING, THE, HASH-VALUE, OF, h, COUNT, new delegate(object
o) { (string o).StartsWith("foo"); }, COLLECT ....
m
"mgonzales3" <mg********@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
I have a variable: int nbr= 5.
I need to test if the nbr is <=10, between 11 and 15, between 16 and 20 or
greater than 20.
How can I do this?
thanks.