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 / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

atoi() equivilent without exceptions?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Ching - 03 Mar 2007 17:46 GMT
Hello,

I'm a .NET newbie but have 12 years of C++/MFC.

In C++, atoi() stops converting at the first non-digit, so in this code,
octet will be 192:

   int octet = atoi("192.");    // <-- note '.' at end of this string

This is great, exactly what I want.  But to achieve it in C++/CLI, the code
I am using is this:

  try
  {
      int octet = Convert::ToInt16("192.");
  }
  catch (Exception^ )
  {
      // Convert::ToInt16 throws when there are non-digits in parameter
  }

Because an exception is thrown, the conversion is not successful.  This is
highly unusable!  I've had to strip off the '.' so Convert::ToInt16() is
successful in the normal case.

However, if the string is malformed (doesn't contain any digits), the
exception is still thrown, whereas atoi() simply returns a value which I
skip.  Maybe I'm misunderstanding exceptions (I don't use them even in C++
when I can help it), but having to try/catch an exception instead of just
getting a simple return value seems to be adding complexity instead of
reducing it.  I don't like replacing 1 line of C++ with 8 lines of .NET for
a simple conversion.

Is there a better way?

Thanks,
David (MVP - VC++)
David Lowndes - 03 Mar 2007 19:47 GMT
>Is there a better way?

Since it's C++/CLI, keep using atoi.

Dave
Willy Denoyette [MVP] - 03 Mar 2007 21:59 GMT
> Hello,
>
[quoted text clipped - 32 lines]
> Thanks,
> David (MVP - VC++)

Use the TryParse method like this:

int result;
if(Int32::TryParse("129.", result) == true)
  Console::WriteLine("{0}", result);
else
   ...
or the overload which allows you specify a Number style or/and a FormatProvider.

Willy.
David Ching - 04 Mar 2007 02:18 GMT
> Use the TryParse method like this:
>
[quoted text clipped - 3 lines]
> else
>    ...

Thanks much Willy it worked somewhat.  TryParse() returned false for "129."
since it didn't like the '.' at the end.  Only whitespace is allowed after
the digits.  But if I kept my code to remove the '.', then it worked, and I
don't have to try/catch an exception, saving code.

I need to look for the methods starting with "Try", as these seem to be the
ones that don't throw exceptions.

Thanks again,
David (MVP - VC++)
Jochen Kalmbach [MVP] - 04 Mar 2007 08:27 GMT
Hi David!

>>Use the TryParse method like this:
>>
[quoted text clipped - 8 lines]
> the digits.  But if I kept my code to remove the '.', then it worked, and I
> don't have to try/catch an exception, saving code.

Take a look at the "NumberStyles" enumeration!

The following works without any modification:

  int i = 0;
  Console::WriteLine(
    Int32::TryParse("129.",
    Globalization::NumberStyles::Number,
    nullptr,
    i)
  );
  Console::WriteLine(i);

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

David Ching - 04 Mar 2007 17:16 GMT
> Take a look at the "NumberStyles" enumeration!
>
[quoted text clipped - 8 lines]
>   );
>   Console::WriteLine(i);

Thanks for the tip Jochen!  Much appreciated.

-- David

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.