tricky C# literals
-
And how about
0x80070020u
?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.
-
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!
That's nice trick with unchecked. Thanks for sharing. I eventually used decimal format, it's just a number after all.
-
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 boolThere 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.
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
-
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
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
-
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
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.
-
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
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
-
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.
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
-
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 boolThere 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.
-
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
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!
-
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!
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