Follow @RoyOsherove on Twitter

IsNumeric() hell

As an old VB'er I have to ask - what's up with the lack of IsNumeric() in the base class library? Why is there a Char.IsNumber() but no string.IsNumeric()? Why should I have to reference Microsoft.VisualBasic.Dll for this “special” feature? (BTW - there's no harm in referencing that dll, it's a part of the framework just like System.XML is, but still...)

Here's a long and thorough discussion on the various “workarounds” needed to have this simple functionality implemented. Highlights:

  • Use the Microsoft.VisualBasic namespace(easy and clean, but why so far away?)
  • Convert the string to a char array and check each item using Char.IsNumber()
  • Convert the string to a char array and check each item using if (arr[i] > 57 || arr[i] < 48) return false;
  • Try a conversion and use a Try-Catch block to check for no- success(the worst performance option)
  • Use Double.TryParse() to return a boolean for success (now why don't we have this for the other types?)
  • (added later:) Yeah, you can also use a regular expression to check for a numeric.

Are we done yet?

This reminds me of a joke (which you surely know):

NASA spent $12 million for a space pen that can write upside down while the russians just used a pencil.

I'm in the "contributor spotlight" on MSDNAA

Interesting research