Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2004

Tip: Looking for answers? Try searching our database.

Numeric Validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 21 Apr 2004 18:36 GMT
I have a C# Windows Form project that I want to validate some incoming data.  In the past I have embedded the convert.#### to what ever numeric form I needed in a try catch statement.  Once again I need to do the same thing and have been searching MSDN for a way.  It might be that I am simply over looking it, but is there a function under the CLR or someplace else that I can do like "isnumber" and check it's contents?
Bob - NEVER MIND I FOUND ANOTHER POST - 21 Apr 2004 18:51 GMT
Where somebody under a different group asked the same question
Shri Borde [MSFT] - 24 Apr 2004 00:58 GMT
Int32.Parse(string)  will throw a FormatException. Dont know if there is a
better way to do this.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemint32classparsetopic1.asp

Shri Borde [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> Thread-Topic: Numeric Validation
> thread-index: AcQnxx5yLfhEhzVLRR2ptZNzgzisAg==
[quoted text clipped - 20 lines]
>
> I have a C# Windows Form project that I want to validate some incoming data.  In the past I have embedded the convert.#### to what ever numeric
form I needed in a try catch statement.  Once again I need to do the same
thing and have been searching MSDN for a way.  It might be that I am simply
over looking it, but is there a function under the CLR or someplace else
that I can do like "isnumber" and check it's contents?
Kit George (MSFT) - 23 Jun 2004 19:09 GMT
The best way is TryParse: it's more performant, and I find, easier to use.
Note that TryParse is currently ONLY available on Double, but if you keep
an eye out for beta1 bits of 2.0 (to be released shortly) you will find we
have added TryParse to all the base types.
William Stacey [MVP] - 24 Apr 2004 08:04 GMT
"The Double.TryParse method is like the Parse method, except this method
does not throw an exception if the conversion fails."  You can then do range
checking on the "out" double to validate smaller things like ushort, int ,
etc.

Signature

William Stacey, MVP

> I have a C# Windows Form project that I want to validate some incoming data.  In the past I have embedded the convert.#### to what ever numeric
form I needed in a try catch statement.  Once again I need to do the same
thing and have been searching MSDN for a way.  It might be that I am simply
over looking it, but is there a function under the CLR or someplace else
that I can do like "isnumber" and check it's contents?
Justin Rogers - 24 Apr 2004 09:00 GMT
I have a library that is 4 to 5x faster in normal operation than double.TryParse
and
has a per data type version.  You can check out the code for yourself, and you
are
more than welcome to use it.  Note that in Whidbey they've added TryParse
methods
to all of the numeric data types, but the rolled code I'm using below is still
on an
order of 3 times or more faster depending on the data type being used.

http://weblogs.asp.net/justin_rogers/articles/104504.aspx

Signature

Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

> "The Double.TryParse method is like the Parse method, except this method
> does not throw an exception if the conversion fails."  You can then do range
[quoted text clipped - 7 lines]
> over looking it, but is there a function under the CLR or someplace else
> that I can do like "isnumber" and check it's contents?
Anthony Moore - 18 May 2004 00:20 GMT
I own the numeric types in the Base Class Libraries, and I should comment
on this.

Functionality-wise, the original request was for the abilty to parse
without using Try/Catch. V1.0 had a TryParse method only for the Double
class. In Whidbey, all the numeric types have this functionality.

This code is indeed well optimized and it is not surprising to me that it
performs significantly faster than the current .NET Framework
implementation. However, it should be born in mind that it is not
functionally equivalent. It only works with the integer numeric types, and
it does not support all the globalization options of
System.Globalization.NumberFormatInfo, so it will not work for all cultures.

This explains only some of the performance difference, of course. The bulk
of it is for the following reasons:
1. We have never tried to optimize number parsing in general. This is just
a matter of time and reasources and sometimes space and maintenance
trade-offs. So far we have done some minor tweaks to number parsing in
Whidbey so that it will be about 20% faster, but we have not been
optimizing it specifically yet.
2. The parsing code is actually mostly shared accross all the numeric data
types. This means that parsing an In32 uses a lot of the same logic for
parsing as Double. This is good for reliability, maintenance and code size,
but there are a lot of optimizations you miss out on. We may create a
special-case parser for integer types at some point.

Feedback on whether number parsing is really showing up as a bottleneck in
real-world applications is always welcome, of course, as it helps us make
these sorts of decisions.

--------------------
| From: "Justin Rogers" <Justin@games4dotnet.com>
| References: <A0FF81CA-11AE-4C07-8D13-AB8051E4076E@microsoft.com>
<ufH98qcKEHA.4032@TK2MSFTNGP10.phx.gbl>
| Subject: Re: Numeric Validation
| Date: Sat, 24 Apr 2004 01:00:05 -0700
[quoted text clipped - 7 lines]
| NNTP-Posting-Host: 209-189-203-158-justinrogers.2alpha.com 209.189.203.158
| Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP11.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.clr:10379
| X-Tomcat-NG: microsoft.public.dotnet.framework.clr
[quoted text clipped - 22 lines]
| > over looking it, but is there a function under the CLR or someplace else
| > that I can do like "isnumber" and check it's contents?
cody - 18 May 2004 21:54 GMT
> I own the numeric types in the Base Class Libraries, and I should comment
> on this.
>
> Functionality-wise, the original request was for the abilty to parse
> without using Try/Catch. V1.0 had a TryParse method only for the Double
> class. In Whidbey, all the numeric types have this functionality.

I have a suggestion for whidbey: what about

Nullable<int> TryParse(string s)

returning nullable would be a good Idea, additionally to

bool TryParse(string s, out int val)

what do you think, or was that planned anyway?

Additionally, I'd like it if bool, all enum-types and DateTime would have an
TryParse method included as well.

Signature

cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.