If the
IsChNoSpace
variable is a string or a char, you have to do this:
int x;
if (ToInt32.TryParse(IsChNoSpace, out x))
{
if (x >= 48 && x <= 57)
{
}
}
Beyond that, you're using the wrong operator in your orginal comparison:
if (IsChNoSpace >= 48 & IsChNoSpace <= 57)
should be
if (IsChNoSpace >= 48 && IsChNoSpace <= 57)
EDIT (regarding your choice of operators) =======================
In this instance, yes, you're right, because you're simply comparing intrinsic values. HOWEVER, if you are doing something like this:
if (object != null & object.XYZ < 30)
...C# will throw a null reference exception on "object" because it evaluates BOTH expressions, and the second one will throw the exception). In this version of the same code:
if (object != null && object.XYZ < 30)
...you will NOT get the exception.
Just because you
CAN do something, doesn't mean you
SHOULD do that something. What surprises me the most is that using a bitwise operator where a logical one is expected doesn't cause the compiler to generate a warning.
Why is it bad practice? because it's NOT STANDARD. It creates problems where understanding the code is concerned because you didn't comment the code when you originally wrote it. If you were working on a project I was the lead on, and this kind of thing was discovered during a code review, I would demand that it be changed, and you'd get an official reprimand in your personnel file. And yeah - I'm a hard-ass.