Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« IsNumeric() hell | Main | Interesting research »
Sunday
Sep142003

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.

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>