Click here to Skip to main content
Licence 
First Posted 15 Nov 2005
Views 79,801
Bookmarked 23 times

Is it really Numeric?

By | 15 Nov 2005 | Article
A bad trap in the IsNumeric() function.

Introduction

This is not a true article, it is just a tip about a "trap" that I discovered in the VB.NET IsNumeric() function and that I need to share with the VB programmers community.

Background

If you used VB6 in the past especially, you would know that the IsNumeric() function is able to determine if a given string is convertible to a number or not. Of course, VB.NET also supports it, as it supports many other legacy VB functions (by referencing Microsoft.VisualBasic.dll). These functions should all be avoided in favor of the .NET Framework counterparts, however many VB programmers still use them. That's why I think it's important you are aware of this trap.

The trap

Let's start with this line of code:

MessageBox.Show(IsNumeric("12345").ToString())

Honestly, do you think it will ever return "False"? No, it always returns "True", doesn't it? Well, try it. Now:

  1. Close your application;
  2. Go to Control Panel / Regional and Language Options;
  3. On the "Regional Options" tab, click on the "Customize..." button;
  4. On the "Customize Regional Options" dialog box, change the "Decimal symbol" and the "Digit grouping symbol" in order to have them identical (for example, a comma in both cases);
  5. Confirm your new settings;
  6. Re-run your application: you'll get "False" as a result.

This behavior is due to the ambiguity you created with these settings in the parsing algorithm that tries to convert your string to a number. Other functions such as CDbl() will crash throwing a quite self-explaining exception in this situation, but not IsNumeric(), which simply gives a wrong result...

Let's not discuss about the logic of setting the "Decimal symbol" equal to the "Digit grouping symbol". And let's not discuss about the fact that a system dialog box allows you this kind of a setting! Also, let's not talk about the users' stupidity (yes, I found a user so stupid!). Let's talk about a workaround, to make your code "safe" in the IsNumeric() execution.

A possible workaround

A good workaround consists in explicitly setting the current thread culture, and - with the culture - programmatically setting the "Decimal symbol" and the "Digit grouping symbol", as in:

Imports System.Globalization
Imports System.Threading
...
Dim ci As New CultureInfo("it-IT")
Dim nfi As New NumberFormatInfo
nfi.CurrencyDecimalSeparator = ","
nfi.CurrencyGroupSeparator = "."
ci.NumberFormat = nfi
Thread.CurrentThread.CurrentCulture = ci

Of course, these settings you are "forcing" programmatically are limited to your application context and they won't influence (hopefully) anything else in the system.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alberto Venditti

Technical Lead

Italy Italy

Member

I was born in 1970.
 
I studied Electronic Engineering (graduated in 1997).
 
Subsequently, I passed some Microsoft exams, and currently I'm certified as:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).
 
My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberMarqW4:41 16 Feb '11  
GeneralAnother way to skin this kind of cat [modified] PinmemberPeterBiddlecombe22:32 22 Nov '07  
GeneralRe: Another way to skin this kind of cat PinmemberAlberto Venditti6:02 23 Nov '07  
QuestionHow to fix this IsNumberic's error in VB6.0? PinmemberTiger.xing20:03 14 Mar '06  
AnswerRe: How to fix this IsNumberic's error in VB6.0? PinmemberAlberto Venditti21:10 23 Mar '06  
GeneralCouldnt you just... Pinmemberil_manti0:37 24 Nov '05  
GeneralRe: Couldnt you just... PinmemberAlberto Venditti3:34 24 Nov '05  
GeneralRe: Couldnt you just... Pinmemberil_manti4:33 24 Nov '05  
GeneralRe: Couldnt you just... Pinmemberunklegwar6:07 28 Nov '05  
GeneralRe: Couldnt you just... PinmemberAlberto Venditti22:22 28 Nov '05  
If I can agree with you about your sentence: "If someone goes and does something dumb, they deserve to have things not work", I disagree about the general "don't bother to check this" approach.
Think about this situation: you developed a software, then sold to many customers. What if they call your support because the application crashes? Okay, after reading this article, probably you would ask them to check their regional settings.
Anyway, your software immediately will seem "poor", because not "tolerant" to this issue.
Then, I think that people don't go every day on the Regional Settings panel, so, probably, when running an application, they are not all completely aware of the running settings, eventually modified by someone else! In this situation your application crash is something seen absolutely as due to the application itself.
 
AV

QuestionAnd Regular Expression? PinmemberWalter O Dias0:25 23 Nov '05  
AnswerRe: And Regular Expression? PinmemberAlberto Venditti21:13 23 Mar '06  
Generalalternative methods to IsNumeric, IsDate, InStr PinmemberAlexis Rzewski2:31 22 Nov '05  
GeneralRe: alternative methods to IsNumeric, IsDate, InStr PinmemberAlberto Venditti23:59 22 Nov '05  
GeneralRe: alternative methods to IsNumeric, IsDate, InStr PinmemberAlberto Venditti0:11 23 Nov '05  
GeneralNaive IsNumeric() inC# PinmemberGuido_d1:57 16 Nov '05  
GeneralRe: Naive IsNumeric() inC# PinmemberMichael Potter3:38 16 Nov '05  
GeneralRe: Naive IsNumeric() inC# PinmemberJared James Sullivan9:36 26 Oct '07  
GeneralRe: Naive IsNumeric() inC# Pinmemberxfx21:53 19 Jan '09  
GeneralRe: Naive IsNumeric() inC# Pinmembernikoniko20:49 23 Nov '05  
GeneralRe: Naive IsNumeric() inC# Pinmemberpercyboy14:13 19 Dec '05  
GeneralImplement Visual Basic .NET IsNumeric Functionality by Using Visual C# .NET PinmemberStanimir Angeloff21:22 15 Nov '05  
GeneralRe: Implement Visual Basic .NET IsNumeric Functionality by Using Visual C# .NET PinmemberAlberto Venditti22:20 15 Nov '05  
GeneralBetter way IMO Pinmembermav.northwind19:30 15 Nov '05  
GeneralRe: Better way IMO PinmemberAlberto Venditti22:16 15 Nov '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 15 Nov 2005
Article Copyright 2005 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid