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. Other Discussions
  3. The Weird and The Wonderful
  4. Facepalm of the day.

Facepalm of the day.

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasecomquestion
24 Posts 11 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.
  • P PIEBALDconsult

    Jörgen Andersson wrote:

    doubt that TryParse would have made a difference.

    Fewer puppies would have been killed. Don't use Convert.

    J Offline
    J Offline
    Jorgen Andersson
    wrote on last edited by
    #10

    If I don't expect the conversion to fail I want an exception. When failure is expected I use TryParse. There is a reason both exists.

    Wrong is evil and must be defeated. - Jeff Ello[^]

    P 1 Reply Last reply
    0
    • J Jorgen Andersson

      If I don't expect the conversion to fail I want an exception. When failure is expected I use TryParse. There is a reason both exists.

      Wrong is evil and must be defeated. - Jeff Ello[^]

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #11

      Yes, in this case Parse is more appropriate. (I just updated my earlier response with an example.)

      1 Reply Last reply
      0
      • J Jorgen Andersson

        And we have a winner. Amazing that the decimal point is making the conversion more than 3000 times slower. But since it didn't throw an exception but rather make a correct conversion I actually doubt that TryParse would have made a difference.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #12

        Jörgen Andersson wrote:

        But since it didn't throw an exception but rather make a correct conversion

        What version of .NET? I've just tried Convert.ToInt32("42.") in LINQPad for 3.5 and 4.5, and both versions throw a FormatException.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        J 1 Reply Last reply
        0
        • L Lost User

          Your length is off by one. The decimal point is included. I prefer int.TryParse over Convert.

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #13

          That slows it down? I spotted that right away, about 15 mins after he posted, but figured it'd be irrelevant, so didn't post. So a trailing decimal has that much of an effect... Good to know.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          1 Reply Last reply
          0
          • J Jorgen Andersson

            And we have a winner. Amazing that the decimal point is making the conversion more than 3000 times slower. But since it didn't throw an exception but rather make a correct conversion I actually doubt that TryParse would have made a difference.

            Wrong is evil and must be defeated. - Jeff Ello[^]

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #14

            3000 times slower? I noticed the extra character about 15 mins after you started this thread, but didn't think it relevant. I'll have to keep an eye out for that... Almost as bad as the exponent operator in VB.NET... (Try benchmarking 2^2 vs Math.Pow(2,2))

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Jörgen Andersson wrote:

              But since it didn't throw an exception but rather make a correct conversion

              What version of .NET? I've just tried Convert.ToInt32("42.") in LINQPad for 3.5 and 4.5, and both versions throw a FormatException.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #15

              4.5

              Wrong is evil and must be defeated. - Jeff Ello[^]

              Richard DeemingR 1 Reply Last reply
              0
              • J Jorgen Andersson

                4.5

                Wrong is evil and must be defeated. - Jeff Ello[^]

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #16

                Weird. If I try Convert.ToInt32("42.") in .NET 4.5, I get: FormatException: Input string was not in a correct format. Are you sure you're not doing something else in your code that's hiding the error?


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                J R 2 Replies Last reply
                0
                • Richard DeemingR Richard Deeming

                  Weird. If I try Convert.ToInt32("42.") in .NET 4.5, I get: FormatException: Input string was not in a correct format. Are you sure you're not doing something else in your code that's hiding the error?


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #17

                  Odd, I'have to check on monday when I'm back at work.

                  Wrong is evil and must be defeated. - Jeff Ello[^]

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Weird. If I try Convert.ToInt32("42.") in .NET 4.5, I get: FormatException: Input string was not in a correct format. Are you sure you're not doing something else in your code that's hiding the error?


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    R Offline
                    R Offline
                    Robert Rohde
                    wrote on last edited by
                    #18

                    Jörgen Andersson (is this you "Neo"?!?) sounds scandinavian and they probably have a different decimal separator... Robert

                    Richard DeemingR 1 Reply Last reply
                    0
                    • R Robert Rohde

                      Jörgen Andersson (is this you "Neo"?!?) sounds scandinavian and they probably have a different decimal separator... Robert

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #19

                      Good point. However, Convert.ToInt32("42,") still produces a FormatException. Trying "42." with the "sv-SE" culture, which uses "," as the decimal separator and "." as the group separator, also produces a FormatException.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      • J Jorgen Andersson

                        So I was building an application that presents a list of files matched with info in a database. Everything was working fine and dandy but horrendously slow. I quickly found the culprit to be in this function:

                        Func GetIDPart_Of_FileName = FileName => Convert.ToInt32(FileName.Substring(FileName.IndexOf('_') + 1, FileName.IndexOf('.') - FileName.IndexOf('_')));

                        The files are built in the form of ID_AnotherID.Extension and what I wanted was AnotherID Can anyone see what I did wrong? :) Do note that it was giving correct results. fixed typo

                        Wrong is evil and must be defeated. - Jeff Ello[^]

                        O Offline
                        O Offline
                        Omar Gameel Salem
                        wrote on last edited by
                        #20

                        hmm FileName.IndexOf('_') is calculated twice too

                        1 Reply Last reply
                        0
                        • J Jorgen Andersson

                          So I was building an application that presents a list of files matched with info in a database. Everything was working fine and dandy but horrendously slow. I quickly found the culprit to be in this function:

                          Func GetIDPart_Of_FileName = FileName => Convert.ToInt32(FileName.Substring(FileName.IndexOf('_') + 1, FileName.IndexOf('.') - FileName.IndexOf('_')));

                          The files are built in the form of ID_AnotherID.Extension and what I wanted was AnotherID Can anyone see what I did wrong? :) Do note that it was giving correct results. fixed typo

                          Wrong is evil and must be defeated. - Jeff Ello[^]

                          M Offline
                          M Offline
                          Marc Clifton
                          wrote on last edited by
                          #21

                          Well, a little late to the party, but this is why I have a whole bunch of string extensions, like this "Between" and "ToInt32": FileName.Between('_', '.').ToInt32() And then you would never have included the '.' by accident. ;) Marc

                          Imperative to Functional Programming Succinctly Higher Order Programming

                          J G 2 Replies Last reply
                          0
                          • M Marc Clifton

                            Well, a little late to the party, but this is why I have a whole bunch of string extensions, like this "Between" and "ToInt32": FileName.Between('_', '.').ToInt32() And then you would never have included the '.' by accident. ;) Marc

                            Imperative to Functional Programming Succinctly Higher Order Programming

                            J Offline
                            J Offline
                            Jorgen Andersson
                            wrote on last edited by
                            #22

                            Just put that on my todo list. :thumbsup:

                            Wrong is evil and must be defeated. - Jeff Ello[^]

                            M 1 Reply Last reply
                            0
                            • J Jorgen Andersson

                              Just put that on my todo list. :thumbsup:

                              Wrong is evil and must be defeated. - Jeff Ello[^]

                              M Offline
                              M Offline
                              Marc Clifton
                              wrote on last edited by
                              #23

                              Send me a direct email and I'll send you the .cs file of my extensions. There's this[^] too. Marc

                              Imperative to Functional Programming Succinctly Higher Order Programming

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                Well, a little late to the party, but this is why I have a whole bunch of string extensions, like this "Between" and "ToInt32": FileName.Between('_', '.').ToInt32() And then you would never have included the '.' by accident. ;) Marc

                                Imperative to Functional Programming Succinctly Higher Order Programming

                                G Offline
                                G Offline
                                Garth J Lancaster
                                wrote on last edited by
                                #24

                                very cool (aka :cool: ) 'g'

                                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