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. Whats wrong with this code???

Whats wrong with this code???

Scheduled Pinned Locked Moved The Weird and The Wonderful
beta-testinghelpquestion
14 Posts 9 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.
  • A Offline
    A Offline
    Ankush Bansal
    wrote on last edited by
    #1

    I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

    private static double ConvertToDouble(object obj, out bool good)
    {
    if (obj is double || obj is int)
    {
    good = true;
    return (double)obj;
    }

            good = false;
            return double.NaN;
        }
    

    This code exploded in to

    Exception type: InvalidCastException
    Message: Specified cast is not valid.
    [System.InvalidCastException]

    V B OriginalGriffO S E 7 Replies Last reply
    0
    • A Ankush Bansal

      I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

      private static double ConvertToDouble(object obj, out bool good)
      {
      if (obj is double || obj is int)
      {
      good = true;
      return (double)obj;
      }

              good = false;
              return double.NaN;
          }
      

      This code exploded in to

      Exception type: InvalidCastException
      Message: Specified cast is not valid.
      [System.InvalidCastException]

      V Offline
      V Offline
      vonb
      wrote on last edited by
      #2

      It failed converting int to double?

      The signature is in building process.. Please wait...

      A 1 Reply Last reply
      0
      • A Ankush Bansal

        I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

        private static double ConvertToDouble(object obj, out bool good)
        {
        if (obj is double || obj is int)
        {
        good = true;
        return (double)obj;
        }

                good = false;
                return double.NaN;
            }
        

        This code exploded in to

        Exception type: InvalidCastException
        Message: Specified cast is not valid.
        [System.InvalidCastException]

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        And float and uint are also really bad...

        A 1 Reply Last reply
        0
        • V vonb

          It failed converting int to double?

          The signature is in building process.. Please wait...

          A Offline
          A Offline
          Ankush Bansal
          wrote on last edited by
          #4

          Yes!! Actually the question was raised by the same colleague when I told him its wrong.... :-D

          1 Reply Last reply
          0
          • B Bernhard Hiller

            And float and uint are also really bad...

            A Offline
            A Offline
            Ankush Bansal
            wrote on last edited by
            #5

            Well in theory not... but our middleware doesn't support uint and float so for us (practically) they are bad.

            1 Reply Last reply
            0
            • A Ankush Bansal

              I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

              private static double ConvertToDouble(object obj, out bool good)
              {
              if (obj is double || obj is int)
              {
              good = true;
              return (double)obj;
              }

                      good = false;
                      return double.NaN;
                  }
              

              This code exploded in to

              Exception type: InvalidCastException
              Message: Specified cast is not valid.
              [System.InvalidCastException]

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              That's what I call good quality, well tested code! :doh: It's obvious it should have been:

                          return (double)(int)obj;
              

              :laugh:

              The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                That's what I call good quality, well tested code! :doh: It's obvious it should have been:

                            return (double)(int)obj;
                

                :laugh:

                The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                A Offline
                A Offline
                Ankush Bansal
                wrote on last edited by
                #7

                This would fail if obj is double... :-\

                OriginalGriffO 1 Reply Last reply
                0
                • A Ankush Bansal

                  This would fail if obj is double... :-\

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  In which case you make it

                  return (double)(int)(double)obj;

                  :laugh:

                  The only instant messaging I do involves my middle finger. English doesn't borrow from other languages. English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • A Ankush Bansal

                    I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

                    private static double ConvertToDouble(object obj, out bool good)
                    {
                    if (obj is double || obj is int)
                    {
                    good = true;
                    return (double)obj;
                    }

                            good = false;
                            return double.NaN;
                        }
                    

                    This code exploded in to

                    Exception type: InvalidCastException
                    Message: Specified cast is not valid.
                    [System.InvalidCastException]

                    S Offline
                    S Offline
                    Sentenryu
                    wrote on last edited by
                    #9

                    He likes reinventing the well or doesn't he know Convert.ToDouble()[^]?

                    I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                    1 Reply Last reply
                    0
                    • A Ankush Bansal

                      I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

                      private static double ConvertToDouble(object obj, out bool good)
                      {
                      if (obj is double || obj is int)
                      {
                      good = true;
                      return (double)obj;
                      }

                              good = false;
                              return double.NaN;
                          }
                      

                      This code exploded in to

                      Exception type: InvalidCastException
                      Message: Specified cast is not valid.
                      [System.InvalidCastException]

                      E Offline
                      E Offline
                      ENOTTY
                      wrote on last edited by
                      #10

                      It should be called TryConvertToDouble with the signature and semantics following the TryXXX functions like TryGetValue(), that is: It should return bool true on successful conversion, false otherwise and the converted value in the out parameter.

                      S 1 Reply Last reply
                      0
                      • E ENOTTY

                        It should be called TryConvertToDouble with the signature and semantics following the TryXXX functions like TryGetValue(), that is: It should return bool true on successful conversion, false otherwise and the converted value in the out parameter.

                        S Offline
                        S Offline
                        Sentenryu
                        wrote on last edited by
                        #11

                        It shouldn't exist at all.

                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p) "Given the chance I'd rather work smart than work hard." - PHS241 "'Sophisticated platform' typically means 'I have no idea how it works.'"

                        1 Reply Last reply
                        0
                        • A Ankush Bansal

                          I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

                          private static double ConvertToDouble(object obj, out bool good)
                          {
                          if (obj is double || obj is int)
                          {
                          good = true;
                          return (double)obj;
                          }

                                  good = false;
                                  return double.NaN;
                              }
                          

                          This code exploded in to

                          Exception type: InvalidCastException
                          Message: Specified cast is not valid.
                          [System.InvalidCastException]

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Unboxing cannot do a representation-changing conversion.

                          1 Reply Last reply
                          0
                          • A Ankush Bansal

                            I was assigned a QA bug today and I found this horror code written by my colleague :mad:...

                            private static double ConvertToDouble(object obj, out bool good)
                            {
                            if (obj is double || obj is int)
                            {
                            good = true;
                            return (double)obj;
                            }

                                    good = false;
                                    return double.NaN;
                                }
                            

                            This code exploded in to

                            Exception type: InvalidCastException
                            Message: Specified cast is not valid.
                            [System.InvalidCastException]

                            V Offline
                            V Offline
                            Vasudevan Deepak Kumar
                            wrote on last edited by
                            #13

                            Perhaps an effect of not using http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx[^]

                            Vasudevan Deepak Kumar Personal Homepage BRAINWAVE/1.0 Status-Code: 404 Status-Text: The requested brain could not be found. It may have been deleted or never installed.
                            --Brisingr Aerowing

                            R 1 Reply Last reply
                            0
                            • V Vasudevan Deepak Kumar

                              Perhaps an effect of not using http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx[^]

                              Vasudevan Deepak Kumar Personal Homepage BRAINWAVE/1.0 Status-Code: 404 Status-Text: The requested brain could not be found. It may have been deleted or never installed.
                              --Brisingr Aerowing

                              R Offline
                              R Offline
                              Rob Grainger
                              wrote on last edited by
                              #14

                              Actually, that's designed to convert a string to double, the code in question is designed to convert objects to double - including explicitly ints. You're not suggesting the way to convert an integer to a double is as follows are you? 1. Convert the int to a string. 2. Convert the string to a double.

                              "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                              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