473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

.NET equivilant of isnumeric

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

--
mo*******@noemail.noemail
Aug 22 '06 #1
8 2567
"moondaddy" <mo*******@noemail.noemailschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 22 '06 #2
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#
"moondaddy" wrote:
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

--
mo*******@noemail.noemail
Aug 22 '06 #3
moondaddy wrote:
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.
Aug 23 '06 #4
>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
Aug 23 '06 #5
>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.
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/jwoole...03/15/788.aspx for
more on this topic.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Aug 23 '06 #6
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" <ji*************@hotmail.comschreef in bericht
news:24*************************@msnews.microsoft. com...
>>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


Aug 23 '06 #7
"Jim Wooley" <ji*************@hotmail.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 23 '06 #8
OK thanks for everyone's feedback above. looks like I'll stick with
isnumeric.

--
mo*******@noemail.noemail
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
"moondaddy" <mo*******@noemail.noemailschrieb:
>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: eje | last post by:
IsNumeric(value) should return false if value "can not be successfully converted to a Double." Instead I get the following error message: "Input string was not in a correct format." I use the...
4
by: Eugene Anthony | last post by:
I have received the following feedback for the two functions bellow: "The ISNUMERIC test is WORTHLESS for checking for an INT value, because ISNUMERIC will happily accept DOUBLE values, such as...
14
by: Kenny | last post by:
Hello, I would like to know if the function IsNumeric requires a header like #include <iostream> to be functionnal thanks ken
8
by: John Bowman | last post by:
Hello, Does anyone have a good/reliable approach to implementing an IsNumeric() method that accepts a string that may represent a numerical value (eg. such as some text retrieved from an XML...
3
by: martin | last post by:
Hi, is there a dotnet function (other than the old isnumeric from VB) to check whether an object is numeric or not. also I notice that all the old vb functions such as split / isnumeric /...
7
by: Nathan Truhan | last post by:
All, I think I may have uncovered a bug in the IsNumeric function, or at least a misunderstanding on functionality. I am writing a Schedule Of Classes Application for our campus and have a...
12
by: sck10 | last post by:
Hello, I am trying to determine if a value is NOT numeric in C#. How do you test for "Not IsNumeric"? protected void fvFunding_ItemInserting_Validate(object sender, FormViewInsertEventArgs...
12
by: Paul | last post by:
Hi, I am trying to check a string to see if it's first 3 characters are numeric and if they are, to replace those 3 characters with something else. I've tried this but nothing happens... ...
17
by: MLH | last post by:
I have tested the following in immed window: ?isnumeric(1) True ?isnumeric(1.) True ?isnumeric(1.2) True ?isnumeric(1.2.2)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.