|
-
Aug 17th, 2005, 09:30 PM
#1
Thread Starter
PowerPoster
quick Q about ASCII
Hi
im trying to see if the current character is between 2 values in the ASCII table (numeric). if they are then I wish to continue. It works except when you enter a value of less than the ASCII value of 57 it assumes that it's true when it really isnt
how can i tell it to see if the value entered is between the numbers in the ASCII table?
Code:
foreach(char curChar in txtPortNumber.Text)
{
//is it a number?
if (curChar >= 48 && curChar <= 57 == false )
{
MessageBox.Show("Text entered is not numeric");
return false;
}
else
{
fullString += curChar;
if (Convert.ToInt32(fullString) > 65535)
{
MessageBox.Show("Ports cannot be greater than 65535");
return false;
}
}
}
-
Aug 18th, 2005, 12:17 AM
#2
Re: quick Q about ASCII
Get rid of this bit:" == false". These two expressions: "curChar >= 48" and "curChar <= 57" already evaluate to boolean values so there is no need to compare either or both of them to a boolean literal. Anyway, why not use Double.TryParse to check whether a string represents a numeric value.
-
Aug 18th, 2005, 12:23 AM
#3
Thread Starter
PowerPoster
Re: quick Q about ASCII
thanks, ill try that too but found another way and seems to work fine
-
Aug 18th, 2005, 12:45 AM
#4
Re: quick Q about ASCII
Code:
double tmpPrtNo;
if (!double.TryParse(txtPortNumber.Text, System.Globalization.NumberStyles.Integer, null, tmpPrtNo))
{
MessageBox.Show("Text entered is not numeric");
return false;
}
if(tmpPrtNo > 65535)
{
MessageBox.Show("Ports cannot be greater than 65535");
return false;
}
int portNumber = Convert.ToInt32(tmpPrtNo);
You might also consider using a NumericUpDown control with a Maximum of 65535 so vaildation is not necessary.
-
Aug 18th, 2005, 04:08 AM
#5
Re: quick Q about ASCII
char.IsDigit(curChar);
I don't live here any more.
-
Aug 18th, 2005, 12:30 PM
#6
Re: quick Q about ASCII
lol hehe
dunno whats going on, but as mentioned above, I would "strongly" suggest usign Double.TryParse if you're just trying to see if a string is a number
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|