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.

17 thoughts on “looking for isnumeric in vb.net?”

  1. 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. Or even simpler:

    Imports System.Text.RegularExpressions

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

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

  4. 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….

  5. 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

  6. 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.

  7. 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

  8. 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

  9. 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

  10. oops. here’s the correct one.

    public function IsNumeric(ByVal textz as string) as boolean
    dim ch as boolean
    for each c as byte in System.Text.Encoding.ASCII.getbytes(text)
    if (48 <= c <= 57) then
    c2 = true ‘good job.
    else
    c2 = false ‘bad job ):
    return false
    exit function
    end if
    next
    return c2
    end function

  11. in the keypress event of the textbox, you could add

    Dim mychar As Char
    mychar = e.KeyChar
    If IsNumerical(mychar.ToString) = True Then
    ‘Insert positive code here
    else
    msgbox(“Dammit I said numbers only”)

    This checks each character as its passed to the txtbox,

  12. Integer.TryParse(MyText, MyDestNumber)
    OR
    if Integer.TryParse(MyText, MyDestNumber) then …

    if you need decimal values then
    Double.TryParse(MyText, MyDestNumber)
    OR
    if Double.TryParse(MyText, MyDestNumber) then …

  13. Use this to the textbox KeyPress event, it accept only number and maximum one “.” and ignore others without add into it:

    Private Sub txtMyTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSSV.KeyPress

    Select Case e.KeyChar
    Case Chr(Keys.Back)
    e.Handled = False
    Case “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “.”
    Try
    Dim strOutput As Double = 0.0
    If Double.TryParse(txtSSV.Text & e.KeyChar, strOutput) Then
    e.Handled = False
    Else
    e.Handled = True
    End If
    Catch ex As Exception
    e.Handled = True
    End Try
    Case Else
    e.Handled = True
    End Select

    End Sub

  14. As with all VB coding, you do NOT have to import a Namspace to in order to use it. Just call that namespace.function:

    Microsoft.VisualBasic.IsNumeric(expression)

    Besides, as Carter says .. if you’re programming in the .NET framework in VB (which if you are programming in VS2003+, then you have no choice), just inherit/import the Microsoft/System namespaces .. You have to the .NET framework installed on the computer anyways to run the program, so you don’t have to worry about NOT having a particular DLL

Comments are closed.