• Advertisement
Sign in to follow this  

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

This topic is 4672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement