I have a nullable integer property in vb.net.
This property in code has correct value, but in the QuickWatch
always display 1
Confirmed, this is indeed a bug in the debugger. I searched connect.microsoft.com and could not find anything similar, this is not a bug that strikes many VB.NET programmers. Not entirely surprising, this only goes wrong when you append .ToString() to the nullable variable name. Nobody ever does that.
It is not the kind of bug you can get any help with at this site, it is a bug that Microsoft needs to fix.
Characterizing the bug a bit, this appears to go wrong in the VB.NET specific expression parser built into the debugger. The reason that you can't repro this in a C# project. And why the bitness of the process doesn't matter, the 64-bit debugger also shows bad values. This is in general a fickle piece of code that Microsoft has been working on to retire. Basic problem is that they had to build the equivalent of the VB.NET compiler into the debugger so that these expressions could be parsed. Limited though, that parser does not nearly support the entire language. Side-effect is that the code that debugger runs can be different than the code your program runs.
The code-generation for Nullable(Of T).ToString() is fairly tricky, it requires a conditional boxing conversion. The parser flubs it for any such expression, note how MyNumber.GetHashCode()
produces the wrong value as well. And MyNumber.Equals(5456.0)
. The kind of expressions that require that boxing conversion.
You can technically report this bug at connect.microsoft.com as well but I would not recommend spending the time. As noted, Microsoft has been working on retiring this parser and that finally happened. Powered by Roslyn, a compile service that's usable anywhere. It was integrated into VS2015 and as far as know the debugger uses it as well. Not 100% sure, I'll know in 9 days when VS2015 is released. Maybe somebody that has the beta/RC edition installed can confirm with a comment.
UPDATE: confirmed fixed on VS2015.
Meanwhile, until you can update, the workaround is to just stop using ToString() on nullable variables in your quick/watch expressions. It is buggy.