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. tricky C# literals

tricky C# literals

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharplearning
13 Posts 6 Posters 1 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.
  • T Tomas Takac

    I have a code where I need to get an exclusive lock on a file. It goes something like this:

    try
    {
    // open file
    }
    catch(IOException ex)
    {
    if(ex.HResult == 0x80070020)
    {
    // retry
    }
    else
    {
    throw;
    }
    }

    Looked good so I tested and it didn't work at all. So I debugged it and what i saw was:

    ex.HResult 0x80070020 int
    ex.HResult == 0x80070020 false bool

    There is something wrong about the value 0x80070020. So I tried to declare a constant:

    const int lockedFileHResult = 0x80070020;

    And there it was: Cannot implicitly convert type 'uint' to 'int'. Of course because that is actually a negative number but the compiler doesn't know that so it cannot fit it into an integer. Next time I'll be extra careful with hex literals.

    J Offline
    J Offline
    John Torjo
    wrote on last edited by
    #3

    Exception.HResult Property (System)[^] MS suggests you do something like this:

    const int SecondLevelHResult = unchecked( (int)0x81234567 );

    Best, John

    -- Log Wizard - a Log Viewer that is easy and fun to use!

    T 1 Reply Last reply
    0
    • P PIEBALDconsult

      And how about 0x80070020u ?

      T Offline
      T Offline
      Tomas Takac
      wrote on last edited by
      #4

      It was an uint, and the problem was that I was comparing that to and integer. The integer value is -2147024864 but there is no way to declare that in hex. Hex value always translates to positive number which is 2147942432 in this case. The hex view in debugger fooled me. Edit: ok there is a way :) thanks John.

      1 Reply Last reply
      0
      • J John Torjo

        Exception.HResult Property (System)[^] MS suggests you do something like this:

        const int SecondLevelHResult = unchecked( (int)0x81234567 );

        Best, John

        -- Log Wizard - a Log Viewer that is easy and fun to use!

        T Offline
        T Offline
        Tomas Takac
        wrote on last edited by
        #5

        That's nice trick with unchecked. Thanks for sharing. I eventually used decimal format, it's just a number after all.

        1 Reply Last reply
        0
        • T Tomas Takac

          I have a code where I need to get an exclusive lock on a file. It goes something like this:

          try
          {
          // open file
          }
          catch(IOException ex)
          {
          if(ex.HResult == 0x80070020)
          {
          // retry
          }
          else
          {
          throw;
          }
          }

          Looked good so I tested and it didn't work at all. So I debugged it and what i saw was:

          ex.HResult 0x80070020 int
          ex.HResult == 0x80070020 false bool

          There is something wrong about the value 0x80070020. So I tried to declare a constant:

          const int lockedFileHResult = 0x80070020;

          And there it was: Cannot implicitly convert type 'uint' to 'int'. Of course because that is actually a negative number but the compiler doesn't know that so it cannot fit it into an integer. Next time I'll be extra careful with hex literals.

          D Offline
          D Offline
          den2k88
          wrote on last edited by
          #6

          My 2 cents: do not use that magic number, declare it as a constant and comment it with the meaning. Believe me, this is one of the very few good practices that is ALWAYS a good practice. Magic numbers are tempting because they are fast to code in... and a hell to figure out.

          GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

          T T J 3 Replies Last reply
          0
          • D den2k88

            My 2 cents: do not use that magic number, declare it as a constant and comment it with the meaning. Believe me, this is one of the very few good practices that is ALWAYS a good practice. Magic numbers are tempting because they are fast to code in... and a hell to figure out.

            GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

            T Offline
            T Offline
            TheGreatAndPowerfulOz
            wrote on last edited by
            #7

            While I agree about the magic number, the problem here was one of silent conversion with data loss.

            Decrease the belief in God, and you increase the numbers of those who wish to play at being God by being “society’s supervisors,” who deny the existence of divine standards, but are very serious about imposing their own standards on society.-Neal A. Maxwell You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

            D 1 Reply Last reply
            0
            • D den2k88

              My 2 cents: do not use that magic number, declare it as a constant and comment it with the meaning. Believe me, this is one of the very few good practices that is ALWAYS a good practice. Magic numbers are tempting because they are fast to code in... and a hell to figure out.

              GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

              T Offline
              T Offline
              Tomas Takac
              wrote on last edited by
              #8

              I agree with the constant. I usually do this after I finish developing a method. I admit In this case I'd seen the problem immediately if I declared the constant upfront.

              D 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                While I agree about the magic number, the problem here was one of silent conversion with data loss.

                Decrease the belief in God, and you increase the numbers of those who wish to play at being God by being “society’s supervisors,” who deny the existence of divine standards, but are very serious about imposing their own standards on society.-Neal A. Maxwell You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                D Offline
                D Offline
                den2k88
                wrote on last edited by
                #9

                Yes I understood it - and encountered the problem frequently enough to be my first bet when I saw it. I also saw the solutions of those who commented before me. I just put my tip, also because usually when one declares a constant puts more attention to the type specification, so it is more probable to correctly define it as an unsigned form the start.

                GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

                1 Reply Last reply
                0
                • T Tomas Takac

                  I agree with the constant. I usually do this after I finish developing a method. I admit In this case I'd seen the problem immediately if I declared the constant upfront.

                  D Offline
                  D Offline
                  den2k88
                  wrote on last edited by
                  #10

                  That's precisely why I suggested it :) I tripped over that problem and similar ones many times and usually delcaring all the data and constant firsthand helps me to avoid simple but time-consuming mistakes like this.

                  GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

                  1 Reply Last reply
                  0
                  • T Tomas Takac

                    I have a code where I need to get an exclusive lock on a file. It goes something like this:

                    try
                    {
                    // open file
                    }
                    catch(IOException ex)
                    {
                    if(ex.HResult == 0x80070020)
                    {
                    // retry
                    }
                    else
                    {
                    throw;
                    }
                    }

                    Looked good so I tested and it didn't work at all. So I debugged it and what i saw was:

                    ex.HResult 0x80070020 int
                    ex.HResult == 0x80070020 false bool

                    There is something wrong about the value 0x80070020. So I tried to declare a constant:

                    const int lockedFileHResult = 0x80070020;

                    And there it was: Cannot implicitly convert type 'uint' to 'int'. Of course because that is actually a negative number but the compiler doesn't know that so it cannot fit it into an integer. Next time I'll be extra careful with hex literals.

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #11

                    Hasnt the compiler yelled about it :confused:

                    Press F1 for help or google it. Greetings from Germany

                    1 Reply Last reply
                    0
                    • D den2k88

                      My 2 cents: do not use that magic number, declare it as a constant and comment it with the meaning. Believe me, this is one of the very few good practices that is ALWAYS a good practice. Magic numbers are tempting because they are fast to code in... and a hell to figure out.

                      GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

                      J Offline
                      J Offline
                      John Torjo
                      wrote on last edited by
                      #12

                      While I totally agree with you, this still won't save you in the OP's case:

                      const int a = 5;
                      const uint b = 6;
                      var c = a == b;

                      This compiles. So long story short, if you have a comparison signed with unsigned, the compiler will compile the code (and at warning level 4 = default), won't issue any warning. Best, John

                      -- Log Wizard - a Log Viewer that is easy and fun to use!

                      D 1 Reply Last reply
                      0
                      • J John Torjo

                        While I totally agree with you, this still won't save you in the OP's case:

                        const int a = 5;
                        const uint b = 6;
                        var c = a == b;

                        This compiles. So long story short, if you have a comparison signed with unsigned, the compiler will compile the code (and at warning level 4 = default), won't issue any warning. Best, John

                        -- Log Wizard - a Log Viewer that is easy and fun to use!

                        D Offline
                        D Offline
                        den2k88
                        wrote on last edited by
                        #13

                        Strange - C++ does and as far as I remember C# is much more picky when it comes to warnings. To be clear I usually compile with Visual Studio 6 and the last C# I used is that of VS2008.

                        GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver "When you have eliminated the JavaScript, whatever remains must be an empty page." -- Mike Hankey "just eat it, eat it"."They're out to mold, better eat while you can" -- HobbyProggy

                        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