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 / C# / March 2008

Tip: Looking for answers? Try searching our database.

TryParse?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DoB - 14 Mar 2008 11:24 GMT
Hi,

As far, as I know, this is not a good code:
------------------------------------------
double d = 0;
try
{
   d = double.Parse(str);
}
catch { }
------------------------------------------

What is the recommended substitution? Using TryParse?

Regards,
DoB
Morten Wennevik [C# MVP] - 14 Mar 2008 12:14 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> DoB

Indeed,

double d = 0;
if(!Double.TryParse(str, out d))
{
  // Error handling
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

John Duval - 14 Mar 2008 12:19 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> DoB

Yes:

    public static void Main()
    {
        string str = "not_a_double";
        double d = double.NaN;
        if (!double.TryParse(str, out d))
        {
            // d is now 0.0, handle failure case
        }
    }

John
Ben Voigt [C++ MVP] - 14 Mar 2008 18:26 GMT
>> Hi,
>>
[quoted text clipped - 19 lines]
> string str = "not_a_double";
> double d = double.NaN;

There is no need to set a value before calling TryParse, it is an out param
so the prior value isn't used.

> if (!double.TryParse(str, out d))
> {
[quoted text clipped - 3 lines]
>
> John
John Duval - 14 Mar 2008 18:55 GMT
> >> Hi,
>
[quoted text clipped - 30 lines]
>
> > John

Right, I was highlighting the fact that TryParse will change the value
to 0 if the parsing fails.
Cowboy (Gregory A. Beamer) - 14 Mar 2008 17:12 GMT
TryParse is the preferred method. As Morten has stated, you should handle
cases where you cannot parse out a number from the string input.

One more note: Your code is not necesarily bad, provided you can always
guarantee a number is going in. If it is all your code, and all internally
dependent (ie, there is no external user input that can influence the
string), you might get away with it.

The other option is to test if the string is a number, but since TryParse
does this for you, that would cost extra cycles, except perhaps at the byte
level :-)

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

*************************************************

| Think outside the box!

*************************************************
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> DoB

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.