views:

232

answers:

3

I currently have this line of code which has been working for the past 6 months:

If IsNumeric(txtProductID.Text) Then
   ...do stuff
Else
   Dim msg As String = "Error!"
End If

All of the sudden, no matter what kind of entry is put in txtProductID (including plain numbers), it fails! Is there reason for me to be going crazy over this?

+2  A: 

Try Trim()ing the string before passing it into the function. In addition, rather than using a VB-specific function like IsNumeric, you might try an approach like this:

Dim input as Integer

If Integer.TryParse(txtProductID.Text, input) Then
    ....do stuff with input
Else
    Dim msg as String = "Error!"
End if

If your number is a decimal number, there are corresponding functions on Double and Single as well.

As to the particular reason that IsNumeric is failing, I couldn't tell you. I can tell you, though, that I've always found it helpful to stick to BCL-compliant functions that are language-agnostic rather than language-specific, like IsNumeric, Str, etc.

Adam Robinson
+3  A: 

Kind of a shot in the dark, but one thing to watch for is that maybe someone wrote a private method called IsNumeric within the same class. Are you sure that the code above is executing Microsoft.VisualBasic.IsNumeric()? If you put your cursor on IsNumeric and hit F12 where does the definition point to?

Jeff Widmer
i'm the only one developing and i haven't written another IsNumeric method... thx tho
Jason
A: 

ugh... i'm an idiot... thanks for your help guys, but apparently i was clearing my whole form before accepting input, so "" will never pass as "IsNumeric". Please don't look at this question again. I feel ill.

Thanks again for your help.

Jason
maybe time to bite the bullet and hit the delete button...
mmr
We all make mistakes like this once in a while. I'm just glad that explaining it here helped you track it down.
Steven Sudit