|
Or, with C# 6:
public int Number { get; } = 42;
public int Number { get; }
public YourClass()
{
Number = 42;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Quote: In this post, we will demonstrate how you can use Google Sign-In with your backend.
Really?
|
|
|
|
|
Well a lot of people use twitter with their backend...
|
|
|
|
|
I have a code where I need to get an exclusive lock on a file. It goes something like this:
try
{
}
catch(IOException ex)
{
if(ex.HResult == 0x80070020)
{
}
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.
|
|
|
|
|
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.
|
|
|
|
|
|
That's nice trick with unchecked. Thanks for sharing. I eventually used decimal format, it's just a number after all.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Hasnt the compiler yelled about it
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
Protected Overridable Function getActivityMarketValue(ByVal act As IActivityRecord, ByVal brokerRecord As IST4Record) As Double
Call getTradeMarketValue(act, brokerRecord)
End Function
I stepped through this about 3 times before I worked out why the activity market value kept being set to zero...
|
|
|
|
|
... Aaaand the reason is... ?
|
|
|
|
|
I was calling the function - not returning it...so even though the right market value was calculated, it never left that function.
|
|
|
|
|
Brilliant VB language definition... A function returns a default value if no return value is specified explicitly...
|
|
|
|
|
It's not just VB which does that
|
|
|
|
|
The beauty of VB...
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Shouldn't that create a compiler error or at least warning as your function doesn't explicitly return a value?
|
|
|
|
|
It gives you a warning:
BC42353 Function 'getActivityMarketValue' doesn't return a value on all code paths. Are you missing a 'Return' statement?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
At least in C# it tells you when you're supposed to return a value.
I don't suppose this would have been caught by turning on VB's "strict" option?
Of course, the last time I did that to some VB code I inherited, I got thousands of errors.
Marc
|
|
|
|