 |
 |

08-06-2012, 08:05 AM
|
 |
Centurion
|
|
Join Date: Jun 2003
Location: New Jersey
Posts: 150
|
|
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
|
|

08-06-2012, 08:45 AM
|
 |
Junior Contributor
|
|
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
|
|
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
|

08-06-2012, 09:04 AM
|
 |
Centurion
|
|
Join Date: Jun 2003
Location: New Jersey
Posts: 150
|
|
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
|
|

08-06-2012, 09:14 AM
|
 |
Junior Contributor
|
|
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
|
|
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
|

08-06-2012, 09:21 AM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,171
|
|
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.
|

08-06-2012, 02:27 PM
|
|
Contributor
* Expert *
|
|
Join Date: Feb 2004
Posts: 522
|
|
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.
|

08-06-2012, 03:22 PM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
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.
|
|

08-06-2012, 04:18 PM
|
 |
Centurion
|
|
Join Date: Jun 2003
Location: New Jersey
Posts: 150
|
|
|
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.
|
|

08-06-2012, 05:50 PM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
|
|
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.
|
|

08-07-2012, 09:58 AM
|
 |
Centurion
|
|
Join Date: Jun 2003
Location: New Jersey
Posts: 150
|
|
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
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|