Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > IsNumeric not working?


Reply
 
Thread Tools Display Modes
  #1  
Old 08-06-2012, 08:05 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default IsNumeric not working?


I have test data where the field 'city' contains 'M3NASHA' (notice the '3' in the second position). I run it through the following code to check to see if the field is numeric for
QCing purposes and it passes when I thought it would cause the msgbox to display. Am I doing this wrong? Shouldn't IsNumeric catch this?

If IsNumeric(city) Then
MsgBox("Error: city should not contain numbers " & vbCrLf & Clean_Rec)
End If


Thanks!
Joan
Reply With Quote
  #2  
Old 08-06-2012, 08:45 AM
TheRealTinTin's Avatar
TheRealTinTin TheRealTinTin is offline
Junior Contributor
 
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
Default

What data type is city? If it is Short, Integer, Long, Decimal, Single, or Double then the function will always return True - see here.

I'm not on a .NET machine just now but if the data type is wrong, I would've expected an error to be returned when trying to assign a non-numeric value to city (assuming it is defined as some sort of number).

Have you tried stepping through the code to check the value of city before it hits the If function?
__________________
Artificial Intelligence is no match for natural stupidity
Reply With Quote
  #3  
Old 08-06-2012, 09:04 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

Thanks for responding

I have city declared as a string...which is what it's supposed to be and that's the reason I'm checking to make sure. If there are any numbers in that field I have to write out the record to an error log.

I did step through the code and 'city' contained exactly what I expected, 'M3NASHA', both before it entered the 'if' statement and after.

Is there another way I should be doing this? How can you check a string to make sure there are no numbers in it?


Thanks!
Joni
Reply With Quote
  #4  
Old 08-06-2012, 09:14 AM
TheRealTinTin's Avatar
TheRealTinTin TheRealTinTin is offline
Junior Contributor
 
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
Default

I can't see why IsNumeric doesn't work then. I will try this later when I get home. As a work around, you could use the Like function with 0-9 in the pattern parameter.
__________________
Artificial Intelligence is no match for natural stupidity
Reply With Quote
  #5  
Old 08-06-2012, 09:21 AM
Qua's Avatar
Qua Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
Default

IsNumeric returns true if the input can be interpreted as a numeric value. In your case, your are giving it a city, which is never a numeric value and thus it always returns false.

But you're not really interested in knowing whether the city name is numeric. What you are actually interested in is knowing whether the name contains any digits. You can check this with a simple regular expression:

Code:
Regex.IsMatch(input, @"\d")
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #6  
Old 08-06-2012, 02:27 PM
hDC_0 hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 522
Default regex and char.IsDigit

Quote:
Originally Posted by Qua
What you are actually interested in is knowing whether the name contains any digits.
..or you could use the Char.IsDigit method.

Just in case anyone is wondering what the difference between Char.IsNumeric and Char.IsDigit --see here.
Here is a sample of the many types of test that can be done with char.

If you want to get a count the number of digits in a string with char.IsDigit,
(or test whether a character in a specified position of a string is a digit),
then you'll find the code here.

In order to get Qua's code to work you should probably include:
Code:
Imports System.Text.RegularExpressions
Here is a tutorial on using Regex.

Last edited by hDC_0; 08-06-2012 at 02:39 PM.
Reply With Quote
  #7  
Old 08-06-2012, 03:22 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

I can't reproduce the behavior you are describing. IsNumeric() is a treacherous function because it uses Val()'s behavior and it can be a little... odd. For example, "200dollars" will be considered numeric because it starts with a number.

There's a few ways to decide if a value is a number.

First it helps to know what the incoming type is. If you know that, you've got a big head start. It's best if it's a string. Then you have the three standard tools:
  • Character-by-character use of IsDigit()
  • Regular expressions
  • The various TryParse() methods.
I like the 3rd choice, because odds are you want the number anyway. The rest are just good for casual, "Is this a number?" checks and you'd still have to do the conversion later.

If you don't know what type it is, you're in a bit of a pickle. *Probably* the best bet is calling .ToString() on whatever you've got to make a string out of it, and try the previous methods on that. If I knew more, I could be more detailed.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 08-06-2012, 04:18 PM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

Thanks everyone for your responses.. I haven't tried any of your suggestions yet but will later on tonight.

AtmaWeapon - the file that I'm checking is a flat text file that I will eventually have to load into a Visual FoxPro table with 35 columns. What I'm trying to do here is just make sure the text is all in the right position before I load it into the table. I'm trying to do this by checking fields that should be all numeric to make sure it contains only digits. I'm doing the same with fields that should only contain letters (like I was trying to do with the field 'city'). That's basically all I'm trying to do. I don't need to perform any calculations or manipulate the data in any way so I'm declaring all the variables for each field as a string.
Reply With Quote
  #9  
Old 08-06-2012, 05:50 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Something like this should do the trick, based heavily on hDC_0's post:

Code:
Function IsANumber(ByVal input As String) As Boolean
    For Each c In input
        If Not Char.IsDigit(c) Then
            Return False
        End If
    Next

    Return True
End Function
A regex could do it in less lines, but that's not the only metric.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #10  
Old 08-07-2012, 09:58 AM
HappyJoni's Avatar
HappyJoni HappyJoni is offline
Centurion
 
Join Date: Jun 2003
Location: New Jersey
Posts: 150
Default

So.. I wound up going with what AtmaWeapon suggested in the last reply. It works great! Thank you so much

I looked into using the Regex suggestion but not 100% on how it works so was a little nervous about using it. I'm definately going to look into it more, seems like it would be a great tool to use once I understand it better.

Thanks everyone!
Joni
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->