.NET equivilant of isnumeric

M

moondaddy

What's the .net framework equivalent of the vb function isnumeric? If there
isn't one, how can I test a string variable to see if its a number or not?
I don't want to use isnumeric if possible
 
H

Herfried K. Wagner [MVP]

moondaddy said:
What's the .net framework equivalent of the vb function isnumeric? If
there isn't one, how can I test a string variable to see if its a number
or not? I don't want to use isnumeric if possible

Why not? Yes, there are possible solutions based on regular expressions or
VB's 'Like' operator, but why bother with implementing your own solution
based on the classes in the .NET Framework if this has already been done by
somebody else?

Note that 'IsNumeric' is not suitable to check if the user has entered a
number in most scenarios because it does not guarantee the number
represented by the string can be stored in a variable of a certain numeric
data type. If you want to parse user input into a certain numeric type, you
may want to use the type's 'Parse' or 'TryParse' method.
 
G

Guest

I second what Herfried says about IsNumeric.
It's a perfectly legit part of the framework and far better tested than
anything you could substitute.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
P

Phill W.

moondaddy said:
What's the .net framework equivalent of the vb function isnumeric? If there
isn't one, how can I test a string variable to see if its a number or not?
I don't want to use isnumeric if possible

If you're writing in Visual Basic, there are just some things you can't
avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim Preserve;
the list goes on.

if you're really set on it, though, your "non-VB" alternative would be
something like:

Try
' substitute Type as required
[Integer].Parse( "value" )
' Yes, it's an Integer
Catch System.FormatException
' No, it's not
End Try

More efficient? Doubt it.
Easier to read? Nope.
Progress? I'll let you make up your own mind.

HTH,
Phill W.
 
J

Jim Wooley

What's the .net framework equivalent of the vb function isnumeric?
If there isn't one, how can I test a string variable to see if its a
number or not? I don't want to use isnumeric if possible
If you're writing in Visual Basic, there are just some things you
can't avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim
Preserve; the list goes on.

if you're really set on it, though, your "non-VB" alternative would be
something like:

Try
' substitute Type as required
[Integer].Parse( "value" )
' Yes, it's an Integer
Catch System.FormatException
' No, it's not
End Try
More efficient? Doubt it.
Easier to read? Nope.
Progress? I'll let you make up your own mind.
HTH,
Phill W.

Using .TryParse over IsNumeric has it's advantages. Particularly in cases
where you want to use the number after you are sure that it is a number.
IsNumeric wraps TryParse. It just throws away the result. If you need ultra
performance, use TryParse natively and use the results rather than throwing
them away and getting them back again (possibly with CInt). Then again, if
you just want to make sure that it is a number, but not process the value,
IsNumeric is fine.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
J

Jim Wooley

What's the .net framework equivalent of the vb function isnumeric?
Why not? Yes, there are possible solutions based on regular
expressions or VB's 'Like' operator, but why bother with implementing
your own solution based on the classes in the .NET Framework if this
has already been done by somebody else?

Note that 'IsNumeric' is not suitable to check if the user has entered
a number in most scenarios because it does not guarantee the number
represented by the string can be stored in a variable of a certain
numeric data type. If you want to parse user input into a certain
numeric type, you may want to use the type's 'Parse' or 'TryParse'
method.

Since IsNumeric is basically a wrapper around TryParse (with a fair amount
of type checking), I'm not sure where you would consider it inferior. I have
noticed that some of the TryParse methods have issues with extra symbols
(like the currency ones) if you use the default method. I would recommend
using the overloads if you need to be sure you are getting the correct results.


See http://devauthority.com/blogs/jwooley/archive/2006/03/15/788.aspx for
more on this topic.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
C

Cor Ligthert [MVP]

Jim,

A user who moves the form one pixel on the screen has probably used more
processor power than your solution will give back in 100 years using your
tryparse solution above the isnumeric.

I have seen these newsgroup full of people writting to keep the UI thread
alive by creating background processes. In your option you should freeze the
UI consequently.

Just my thought reading your message.

Cor

Jim Wooley said:
What's the .net framework equivalent of the vb function isnumeric?
If there isn't one, how can I test a string variable to see if its a
number or not? I don't want to use isnumeric if possible
If you're writing in Visual Basic, there are just some things you
can't avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim
Preserve; the list goes on.

if you're really set on it, though, your "non-VB" alternative would be
something like:

Try
' substitute Type as required
[Integer].Parse( "value" )
' Yes, it's an Integer
Catch System.FormatException
' No, it's not
End Try
More efficient? Doubt it.
Easier to read? Nope.
Progress? I'll let you make up your own mind.
HTH,
Phill W.

Using .TryParse over IsNumeric has it's advantages. Particularly in cases
where you want to use the number after you are sure that it is a number.
IsNumeric wraps TryParse. It just throws away the result. If you need
ultra performance, use TryParse natively and use the results rather than
throwing them away and getting them back again (possibly with CInt). Then
again, if you just want to make sure that it is a number, but not process
the value, IsNumeric is fine.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
H

Herfried K. Wagner [MVP]

Jim Wooley said:
Using .TryParse over IsNumeric has it's advantages. Particularly in cases
where you want to use the number after you are sure that it is a number.
IsNumeric wraps TryParse. It just throws away the result.

It doesn't wrap a .NET Framework type's 'TryParse' method.
 

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