Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How well do you know your TryParse()?

How well do you know your TryParse()?

Scheduled Pinned Locked Moved C#
csharpcomtoolsquestion
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Ravi Bhavnani

    What would you expect foo to be after this code fragment runs?  Post your answer without first peeking at the docs! :-D

    string badNumericString = "Bogus";
    Decimal foo = Decimal.MinValue;
    bool status = Decimal.TryParse (badNumericString, out foo); // status is false, as expected

    Answer:

    • Decimal.MinValue (since the parse failed)
    • Something else (if so, what?)

    /ravi

    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

    D Offline
    D Offline
    dybs
    wrote on last edited by
    #21

    I think foo would be zero, but that's just a guess. I think Int32.TryParse and Double.TryParse set the out parameter to zero if the parse fails, but I don't recall. If TryParse fails, I usually set the value to some known default that I can handle - I don't care what TryParse sets it to, especially is MS decides to change it down the road.

    The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

    R 1 Reply Last reply
    0
    • D dybs

      I think foo would be zero, but that's just a guess. I think Int32.TryParse and Double.TryParse set the out parameter to zero if the parse fails, but I don't recall. If TryParse fails, I usually set the value to some known default that I can handle - I don't care what TryParse sets it to, especially is MS decides to change it down the road.

      The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #22

      You're absolutely correct.

      dybs wrote:

      If TryParse fails, I usually set the value to some known default that I can handle

      I agree that's the advisable thing to do. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups