Results 1 to 6 of 6

Thread: quick Q about ASCII

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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;							
    						}
    					}
    				}

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: quick Q about ASCII

    thanks, ill try that too but found another way and seems to work fine

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: quick Q about ASCII

    char.IsDigit(curChar);


    I don't live here any more.

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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
  •  



Click Here to Expand Forum to Full Width