>= and <=

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I am trying to test if a number is between and certain range. Is there
a clever C# way to do it that is better than this?

if (((Convert.ToInt64(strRequestSpecific)) >=
(Convert.ToInt64(strStartRangeLessZero))) &&
((Convert.ToInt64(strRequestSpecific)) <=
(Convert.ToInt64(strEndRangeLessZero))))

{

}

Thanks,

Mike
 
Hi Mike,

There is no easy 'between' in C# so you will have to do it like you say,
using both above and below statements.
 
Wouldn't the easier way be this?

// Convert to a long.
long lngRequestSpecific = Convert.ToInt64(strReqestSpecific);
long lngStartRangeLessZero = Convert.ToInt64(strStartRangeLessZero);
long lngEndRangeLessZero = Convert.ToInt64(strEndRangeLessZero);

if (lngRequestSpecific >= lngEndRangeLessZero &&
lngStartRangeLessZero <= lngEndRangeLessZero)
{

}

I mean, yes, you are doing pretty much the same thing (actually, one
less convert operation), but the above is easier to read, maintain, type,
etc, etc. And it just looks a hell of a lot prettier too (which,
semi-jokingly, might be the best reason).

Hope this helps.
 
Back
Top