How do I determine is string is number?

S

Saga

Using VB 2008. This seemed like a simple question. To make a short story
long...

Dim ssTest as string

Typed in ssTest<dot> expecting for Intellisense to display something like
IsNumber,
but the only method that seemed related was CompareOrdinal. I selected that
and
then used F1 for more info. This method is something totally different. I
found a few
Isxxxx methods, but none that I could use.

Next up, Google. So how much was Google my friend? In this case, not much. I
found asnwers that ranged from doing a loop and testing each string position
for
0 - 9 (ASCII 48 to 57), to using regular expressions to using the
VisualBasic
namespace function IsNumeric :-S. I also found this routine:

Public Function IsNumber(ByVal sText As String) As Boolean

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

End Function

(http://www.janinedalton.com/blog/archives/2004/looking-for-isnumeric-in-vbnet/)

I am using this function for now. Doesn't VB 2008 have an inherent
function/method
for testing a string to see if it is numeric or not? Thanks! Saga
 
T

Tom Shelton

Using VB 2008. This seemed like a simple question. To make a short story
long...

Dim ssTest as string

Typed in ssTest<dot> expecting for Intellisense to display something like
IsNumber,
but the only method that seemed related was CompareOrdinal. I selected that
and
then used F1 for more info. This method is something totally different. I
found a few
Isxxxx methods, but none that I could use.

Next up, Google. So how much was Google my friend? In this case, not much. I
found asnwers that ranged from doing a loop and testing each string position
for
0 - 9 (ASCII 48 to 57), to using regular expressions to using the
VisualBasic
namespace function IsNumeric :-S. I also found this routine:

Public Function IsNumber(ByVal sText As String) As Boolean

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

End Function

(http://www.janinedalton.com/blog/archives/2004/looking-for-isnumeric-in-vbnet/)

I am using this function for now. Doesn't VB 2008 have an inherent
function/method
for testing a string to see if it is numeric or not? Thanks! Saga

IsNumeric. Though, I personally prefere to use the Double.TryParse.
 
S

Saga

Doesn't VB 2008 have an inherent function/method for testing a >>
IsNumeric. Though, I personally prefere to use the Double.TryParse.

Thanks for your input. Although IsNumeric would be the logical choice
I have read that it is recommended that the developer stay away from the
VisualBasic namespace functionality as this was made available only
to be backwards compatible with VB6. Regards, Saga
 
C

Chris Dunaway

Thanks for your input. Although IsNumeric would be the logical choice
I have read that it is recommended that the developer stay away from the
VisualBasic namespace functionality as this was made available only
to be backwards compatible with VB6. Regards, Saga

Only the "VisualBasic.Compatibility" namespace should be avoided. The
VisualBasic namespace is meant to be used.

Chris
 
T

Tom Shelton

Thanks for your input. Although IsNumeric would be the logical choice
I have read that it is recommended that the developer stay away from the
VisualBasic namespace functionality as this was made available only
to be backwards compatible with VB6. Regards, Saga

You are confusing VisualBasic with VisualBasic.Compatability. VisualBasic is
the core VB language functions and are meant to be used -
VisualBasic.Compatability is there for easier conversion of legacy code and
should be avoided.
 
S

Saga

You are confusing VisualBasic with VisualBasic.Compatability. VisualBasic
is
the core VB language functions and are meant to be used -
VisualBasic.Compatability is there for easier conversion of legacy code
and
should be avoided.
Yes, I am confusing them. Thanks both for the clarafication!
Saga
 
C

Cor Ligthert[MVP]

To make it more confusing and to be before Herfried (which is already for
years answering this question),

You would define was is a number, if you mean it are only characters 0 to 9,
then IsNumeric is not what you want, because it accept also numbers with
exponents and hex numbers.

Cor
 
S

Saga

Thanks for the reply Cor,
You would define was is a number, if you mean it are only characters 0 to
9, then IsNumeric is not what you want, because it accept also numbers
with exponents and hex numbers.

Cor
Yes, you are correct. Knowing the valid data that IsNumeric will classify
as a valid number has kept me away from this function, except for the
simplest of number validation (works great if the data is only 1 char).

Thanks for your input! Saga
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top