473,324 Members | 2,002 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,324 software developers and data experts.

C# equiv to VB IsNumeric ?

or do I have to iterate the string and use Char.IsNumber ?

Nov 15 '05 #1
10 1722
Hi,

You can use Convert.ToInt32/64, Convert.ToDouble, etc.

The class when converting throws an exception if the parameter string is not in a value that can be converted to such the destination type.

It controls as well the ranges (allowed min and max values) and throws an overflow exception.

So this will work without exception:
Convert.ToInt32 ("-34234")

but the following will throw an exception:
Convert.ToInt32 ("-34234897254072502724525")

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
Nov 15 '05 #2
dan
You can actually access all your familiar VB method referencing visual basic within your csharp project
at this time I am not at home, and cannot remember which reference has to be added to your solution in order to have access to the below namespaces

Just normally I do not use this, and instead do a try and catch, and try to convert the numbers using Int32.Parse()...et

Microsoft.VisualBasic.Information.IsNumeric(value)

-- Dan
Nov 15 '05 #3
Hi!,

I think I read somewhere that you shouldn't use exceptions for simple
testing like this - raising exceptions is expensive, so they should only be
used to detect exceptional occurrences.

Maybe somebody can confirm/deny this?
"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com> wrote in
message news:%2******************@tk2msftngp13.phx.gbl...
Hi,

You can use Convert.ToInt32/64, Convert.ToDouble, etc.

The class when converting throws an exception if the parameter string is not
in a value that can be converted to such the destination type.

It controls as well the ranges (allowed min and max values) and throws an
overflow exception.

So this will work without exception:
Convert.ToInt32 ("-34234")

but the following will throw an exception:
Convert.ToInt32 ("-34234897254072502724525")

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

Nov 15 '05 #4
Try using Double.TryParse,
which returns true or false and **does not** throw an exception.
--Peter
"PeteZ" <pe***@aol.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
or do I have to iterate the string and use Char.IsNumber ?

Nov 15 '05 #5
Sheila Jones <sh**********@btopenworld.com> wrote:
I think I read somewhere that you shouldn't use exceptions for simple
testing like this - raising exceptions is expensive, so they should only be
used to detect exceptional occurrences.

Maybe somebody can confirm/deny this?


Well, it's *relatively* expensive - iterating through each character to
check whether or not it's a digit is certainly cheaper, but unless
you're testing millions of them and expecting a large proportion to be
invalid, it's unlikely to make much performance difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

"PeteZ" <pe***@aol.com> wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
or do I have to iterate the string and use Char.IsNumber ?

Nov 15 '05 #7
If you write a function of your own, called IsNumeric, then according to the
..NET guidelines, it should only return true or false and catch any
exceptions that may ocurr, but it can't propagate any exceptions to the
concumer. So you get get either true or you get false but you'll never get
an exception. So you can handle the exception, just don't rely on it. Do
what you can to anticipate it before it happens and he'll be fine.
Thanks,
Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sheila Jones <sh**********@btopenworld.com> wrote:
I think I read somewhere that you shouldn't use exceptions for simple
testing like this - raising exceptions is expensive, so they should only be used to detect exceptional occurrences.

Maybe somebody can confirm/deny this?


Well, it's *relatively* expensive - iterating through each character to
check whether or not it's a digit is certainly cheaper, but unless
you're testing millions of them and expecting a large proportion to be
invalid, it's unlikely to make much performance difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
I think a problem with this is that you will still get thrown in to the
debugger if you're debugging with the 'break into the debugger when an
exception is thrown' option switched on.

I'm still not really convinced about using exceptions as a way to control
ordinary program flow...
"Shawn B." <le****@html.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you write a function of your own, called IsNumeric, then according to the .NET guidelines, it should only return true or false and catch any
exceptions that may ocurr, but it can't propagate any exceptions to the
concumer. So you get get either true or you get false but you'll never get an exception. So you can handle the exception, just don't rely on it. Do
what you can to anticipate it before it happens and he'll be fine.
Thanks,
Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sheila Jones <sh**********@btopenworld.com> wrote:
I think I read somewhere that you shouldn't use exceptions for simple
testing like this - raising exceptions is expensive, so they should
only
be used to detect exceptional occurrences.

Maybe somebody can confirm/deny this?


Well, it's *relatively* expensive - iterating through each character to
check whether or not it's a digit is certainly cheaper, but unless
you're testing millions of them and expecting a large proportion to be
invalid, it's unlikely to make much performance difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #9
I wasn't arguing that exceptions should be used here as a way to control
program flow. Simply that if you create an IsNumeric function, according
the the MS docs on .NET guidelines and style, that the method should only
return true or false but it will never throw an exception. What this means,
is that if an exception ocurrs inside the IsNumeric, that it should be
caught and ultimately return false but the point is that you should code in
such a way to avoid that in the first place. However, the point is even
further that according to MS, it should never throw an exception to the code
consuming the IsXXXX function. It should always handle the exception and
return either true or false. In a way, this is a form of flow control, but
technically, it isn't flow control per se. Just that if an exception
ocurrs, it should catch it and then return false. That's all I was saying,
and what MS was saying.

Thanks,
Shawn
"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I think a problem with this is that you will still get thrown in to the
debugger if you're debugging with the 'break into the debugger when an
exception is thrown' option switched on.

I'm still not really convinced about using exceptions as a way to control
ordinary program flow...
"Shawn B." <le****@html.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
If you write a function of your own, called IsNumeric, then according to

the
.NET guidelines, it should only return true or false and catch any
exceptions that may ocurr, but it can't propagate any exceptions to the
concumer. So you get get either true or you get false but you'll never

get
an exception. So you can handle the exception, just don't rely on it. Do what you can to anticipate it before it happens and he'll be fine.
Thanks,
Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sheila Jones <sh**********@btopenworld.com> wrote:
> I think I read somewhere that you shouldn't use exceptions for simple > testing like this - raising exceptions is expensive, so they should

only
be
> used to detect exceptional occurrences.
>
> Maybe somebody can confirm/deny this?

Well, it's *relatively* expensive - iterating through each character to check whether or not it's a digit is certainly cheaper, but unless
you're testing millions of them and expecting a large proportion to be
invalid, it's unlikely to make much performance difference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #10


public static bool IsNumeric(object value)
{
try
{
double d = System.Double.Parse(value.ToString(),
System.Globalization.NumberStyles.Any);
return true;
}
catch (FormatException)
{
return false;
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #11

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...
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...
8
by: moondaddy | last post by:
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 ...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.