Is this string numeric?

This VB code was brought to my attention.  It is meant to look at a string variable (prefixText) and decide if the string is numeric or not, and return a data set (ds) by using a different method based on the result of the numeric test. (As always, this code is in a heavily used production environment.)

If Left(prefixText, 1) = "0" Or Left(prefixText, 1) = "1" Or Left(prefixText, 1) = "2" Or Left(prefixText, 1) = "3" Or Left(prefixText, 1) = "4" Or Left(prefixText, 1) = "5" Or Left(prefixText, 1) = "6" Or Left(prefixText, 1) = "8" Or Left(prefixText, 1) = "9" Then
    ds = New Users(_ConnStr).getUsersListByNumber(prefixText)
Else
    ds = New Users(_ConnStr).getUsersListByLastName(prefixText)
End If

I have some great ideas on how to improve this code. The first thing I would have done was to use OrElse instead of Or, it would speed things up tremendously. And I would also have added in the test for the “7” character.

4 Comments

  1. John Boker says:

    Everyone knows that a string that starts with 7 is not a number. How could you not know this, you’ve been a developer for how long?

  2. BP says:

    Oh, I don’t know, I would say at least 7 years.

  3. Jerry Evanoff says:

    I would like to suggest a Tegomm to make things run smooth. Brian…thoughts?

  4. BP says:

    All code is better with a Tegomm in it. LOLOLOLOL!!!!!!!!!!!!!!!!!!!!!!!

Leave a Reply