4

Using the .Net Compact Framework 2.0, how can I validate an integer (Int32.TryParse is not supported on the Compact Framework)?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
user690932
  • 419
  • 2
  • 7
  • 19

5 Answers5

3

What do you mean by "validate"? Do you mean parse without throwing?

static bool TryParse(string s, out int value)        
{
    try
    {
        value = int.Parse(s);
        return true;
    }
    catch
    {
        value = 0;
        return false;
    }
}
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • This has its caveats, too. If there are enough non int strings you may have performance issues because exception raising is quite expensive. I once used a similar pattern which slowed down the whole UI under certain circumstances. – Zebi Jul 03 '11 at 17:08
3
  static bool TryParseImpl(string s, int start, ref int value)
  {
    if (start == s.Length) return false;
    unchecked {
      int i = start;
      do {
        int newvalue = value * 10 + '0' - s[i++];
        if (value != newvalue / 10) { value = 0; return false; } // detect non-digits and overflow all at once
        value = newvalue;
      } while (i < s.Length);
      if (start == 0) {
        if (value == int.MinValue) { value = 0; return false; }
        value = -value;
      }
    }
    return true;
  }

  static bool TryParse(string s, out int value)        
  {
    value = 0;
    if (s == null) return false;
    s = s.Trim();
    if (s.Length == 0) return false;
    return TryParseImpl(s, (s[0] == '-')? 1: 0, ref value);
  }

Demo: http://ideone.com/PravA

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

Had same problem. Try this:

static bool numParser(string s)
{
  foreach (char c in s)
    if (!char.IsNumber(c))
      return false;
  return true;
}
Ros
  • 21
  • 1
  • Doesn't account for signs. Also rather use IsDigit. According to MSDN: `In addition to including digits, numbers include characters, fractions, subscripts, superscripts, Roman numerals, currency numerators, and encircled numbers.` – Ral Zarek Oct 11 '12 at 10:35
1
public static bool IsInt(string s) {
    bool isInt = true;
    for (int i = 0; i < s.Length; i++) {
        if (!char.IsDigit(s[i])) {
            isInt = false;
            break;
        }
    }
    return isInt;
}

Example:

string int32s = "10240";
bool isInt = IsInt(int32s); // resolves true

Or:

string int32s = "1024a";
bool isInt = IsInt(int32s); // resolves false
David Anderson
  • 13,558
  • 5
  • 50
  • 76
0

If your number is a string you may get the strings char array and check if Char.IsNumber is true for every char.

Check if the first char is '-' to allow for negative numbers, if you need them and add a try/catch block to protect from numbers exceeding the range (int min / max value). If you don't have to deal with numbers approaching min/max consider setting a max length (maybe 6-7 digits) and simply check string.Length instead.

If the chance you encounter only valid ints and invalid ones are rare invalid operations you may stick with a simple try/catch block (see my comment to ctackes answer).

Zebi
  • 8,682
  • 1
  • 36
  • 42
  • What about `"4697357344197412413772951060481818689355170685471900754944636793"`? – jason Jul 03 '11 at 16:42
  • Depends on the numbers he is expecting. Checking the sign is trivial to add. To protect from really large numbers he may use a try/catch block. This should not occur at all depending on the use case. – Zebi Jul 03 '11 at 17:10
  • 2
    `Char.IsNumber` is definitely the wrong test. "In addition to including digits, numbers include characters, fractions, subscripts, superscripts, Roman numerals, currency numerators, and encircled numbers." – Ben Voigt Jul 03 '11 at 18:20