looking for isnumeric in vb.net?

i was wondering why a vb.net class file was throwing up errors when i called the isnumeric function the other day. after some googling i found out from roy osherove’s blog that you now need to import the microsoft.visualbasic namespace to use this and a bunch of other once standard functions.

as an aside, if you want to pause your program and are wondering why sleep() does not seem to work, you now need to reference system.threading.thread.sleep() instead.

11 Responses to “looking for isnumeric in vb.net?”

  1. Peter Says:

    This little snippet will detect if an input is a numeric value.

    Public Function IsNumeric(ByVal str As String)
    Dim r As Regex = New Regex(”\d+”)
    Dim m As Match = r.Match(str)
    If (m.Success) Then
    Return True
    End If
    Return False
    End Function
    If Not (IsNumeric(TextBox4.Text)) Then
    MessageBox.Show(”A listening port must be a numeric value”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

  2. janine Says:

    thanks for the contribution peter - regular expressions to the rescue once again :)

  3. Kevin Says:

    Or even simpler:

    Imports System.Text.RegularExpressions

    Public Shared Function IsNumeric(ByVal str As String)
    Return New Regex(”\d+”).Match(str).Success
    End Function

  4. Kevin Says:

    oops, make that:
    Public Shared Function IsNumeric(ByVal str As String) as Boolean
    Return New Regex(”\d+”).Match(str).Success
    End Function

  5. Troy Says:

    Unfortunately that regex expression doesn’t work in every case. I am parsing a file and I am getting some non-standard characters at the end of a number because of some bad data that was sent as the power failed. This expression still returns true and my program fails as it tries to convert the number with the bad characters….

  6. Chris Says:

    The proper regex expression should be: ^\d+$
    This will verify that the start of the value is numeric, not just search for any number in the supplied value

  7. DocXango Says:

    I’ve had the best luck with “\b\d+(\.\d+)?\b” as my regular expression pattern since it will match doubles and floats as well.

    My variation on Kevin’s function would be:
    Public Shared Function isNumeric(ByVal textToCheck as String) as Boolean
    return Regex.IsMatch(textToCheck, “\b\d+(\.\d+)?\b”)
    End Function

    Just a bit cleaner in my humble opinion.

  8. JimK Says:

    I get nothing but false positives with the functions listed above

    I created my own and is perfect for detecting ‘all numeric’ values…great for detecting table indexes (just make sure you trim the text first):

    Public Shared Function IsNumeric(ByVal sText As String) As Boolean
    Dim b As Boolean = True
    Dim x As Integer = 0

    For x = 0 To sText.Length - 1
    Dim sChar As String = sText.Substring(x, 1)

    If Asc(sChar) 57 Then
    b = False
    Exit For
    End If
    Next

    Return b
    End Function

  9. JimK Says:

    ok..that didnt paste well…removed some of my code..trying again

    Public Shared Function IsNumeric(ByVal sText As String) As Boolean
    Dim b As Boolean = True
    Dim x As Integer = 0

    For x = 0 To sText.Length - 1
    Dim sChar As String = sText.Substring(x, 1)

    If Asc(sChar) < 48 Or Asc(sChar) > Then
    b = False
    Exit For
    End If
    Next

    Return b
    End Function

  10. Farren Says:

    This seems to work well for me.

    Public Shared Function IsNumeric(ByVal sText As String) As Boolean

    If Double.TryParse(sText, Globalization.NumberStyles.AllowDecimalPoint) Then
    Return True
    Else
    Return False
    End If

    End Function

  11. Carter Says:

    Why reinvent the wheel? IsNumeric is part of .NET. Just use it.

Leave a Reply