Sign in to follow this  
Wavinator

[.net] System::String validation in C++ w/o exception handling?

Recommended Posts

What's the best way to validate whether or not a System::String is numeric without using exceptions? I'm looking for something nice and non-volatile like the Double::TryParse method to determine if a user has entered anything but a number in a text box.

Share this post


Link to post
Share on other sites
If you're working with .NET 2.0, there's also Int32::TryParse and UInt32::TryParse.

If you don't like those (or you're not using .NET 2.0 yet), Double::TryParse will work for floating point numbers, but you'll probably just have to roll your own for integers. I'd wrap it in a helper so that you can replace it with Int32::TryParse easily when you finally do switch to .NET 2.0.

By the way, if you decide to roll your own, Char::IsDigit returns true for any characters which has a "Number" class in Unicode but Int32::Parse only works with the ASCII 0-9, so you have to check for 0-9 explicitly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dean Harding
If you don't like those (or you're not using .NET 2.0 yet), Double::TryParse will work for floating point numbers, but you'll probably just have to roll your own for integers.


If all he's interested is whether something is a number or not, Double::TryParse works perfectly fine: it can parse integers as well as floating-point values. In fact, you can check what it accepts using the style parameter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this