Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
if(IsChNoSpace >= 48 & IsChNoSpace <= 57)

am try to use this condition it shows the follwing error

operator '<=' cannot be applied to operands of type 'string'and 'int'


Please help me
Posted
Updated 17-Jul-11 17:59pm
v2
Comments
Salmen Essridi 17-Jul-11 11:52am    
What is the type of IsChNoSpace ?
RaisKazi 17-Jul-11 11:52am    
What is a type of "IsChNoSpace"?
ken A to Z 17-Jul-11 12:05pm    
string sChNoSpace;
RaisKazi 17-Jul-11 11:53am    
Oh !! Salmen and I Asked the same Question at the same time. Cheers Salmen !!!
Salmen Essridi 17-Jul-11 11:58am    
no matter good job.

this error given you for you declare IsChNoSpace is string. so you want to check with if condition you try this
if(int.Parse(IsChNoSpace)>= 48 && int.Parse(IsChNoSpace)<= 57)
{

}
 
Share this answer
 
Comments
Salmen Essridi 17-Jul-11 11:53am    
yes the error speak :) by it self.
ken A to Z 17-Jul-11 12:02pm    
// i have declare as string
string IsCharNoSpace = null;
IsCharNoSpace = Strings.Asc(Character);

if(IsChNoSpace >= 48 && IsChNoSpace <= 57)

// this error
operator '<=' cannot be applied to operands of type 'string'and 'int'
Salmen Essridi 17-Jul-11 12:10pm    
so you have to parse it. like Rakesh tell.
Sergey Alexandrovich Kryukov 17-Jul-11 23:12pm    
Correct, a 5.
--SA
Hi
you could use the code

char.IsLetterOrDigit(IsChNoSpace) // This is assuming that IsChNoSpace is of type char

to test if your character is a letter and not anything else. This will take into account Caps and small letters though. Not only caps as in your example.
 
Share this answer
 
v2
Comments
paraszalariya12 22-Nov-11 1:48am    
if (sex = "M" || sex = "m") this line display Operator '||' cannot be applied to operands of type 'string' and 'char'

lbl_sex.Text = "sex:male";
else
lbl_sex.Text = "sex:female";
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)
    {
        // ...do something
    }
}

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.
 
Share this answer
 
v3
Comments
RaisKazi 17-Jul-11 11:56am    
My 5.
Salmen Essridi 17-Jul-11 11:56am    
yes but, it is not a problem of operator both & and && can be used.
#realJSOP 17-Jul-11 13:04pm    
Unless there's something about the variable you're not telling us, NO you can't.
Salmen Essridi 17-Jul-11 13:38pm    
Sorry but, i mean that is a problem of parsing not operators.
you can test
int x = 50;
if (x>= 48 & x<= 57)
and it will work fine!
So could you explain why, if not
try this
http://stackoverflow.com/questions/1279217/difference-between-and-or-and-for-comparison
#realJSOP 17-Jul-11 14:48pm    
See my edited answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900