mark mark - 7 months ago 29 Question

Error using isNumeric() in VB

I am using the IsNumeric() function in visual basic 2012.

my code is this

Dim input As String = "123"
If isNumeric(input) Then
'number codes
'not a number codes
End If

and i'm getting an error on the isNumeric(input) part

isNumeric is a namespace and cannot be used as an expression

i just want to know what is wrong with this, i cant find any documentation that this function has already changed or something.


It sounds like you've created a name clash. You have presumably named your project 'IsNumeric'. The root namespace for the project is named after the project by default so you now have a root namespace named 'IsNumeric' and that takes precedence over the IsNumeric method.

There are a number of options to fix this. Firstly, you can change the root namespace for the project to something other than 'IsNumeric', which you would do in the project properties. Alternatively, you can qualify the method name with its namespace, its module or both, i.e. use Microsoft.VisualBasic.IsNumeric, Information.IsNumeric or Microsoft.VisualBasic.Information.IsNumeric.

I'd tend to suggest not using IsNumeric anyway. It can't distinguish between types of numbers and provides no access to the actual numeric value. If you need to do any of that sort of thing then call the appropriate TryParse method instead, e.g.

Dim number As Double

If Double.TryParse(someText, number) Then
    'The input was a valid Double and the value is in 'number'.
    'The input was not a valid Double.
End If

Note that IsNumeric actually calls Double.TryParse internally and is the reason it was created in the first place. That's why calling IsNumeric and then something like CDbl is bad: you're parsing the same text twice in that case.