Bug 2895 - Nullable types not allowing for HasValue check
Summary: Nullable types not allowing for HasValue check
Alias: None
Product: Visual Studio Extensions
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-01-13 15:28 UTC by scott
Modified: 2016-08-03 15:25 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Shows behavior in debugger with nullable types (114.00 KB, image/png)
2012-01-19 16:40 UTC, scott

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description scott 2012-01-13 15:28:22 UTC
I have a nullable double called Latitude.  Observe the following in the command window of the VStudio debugger.

>? Latitude
    HasValue: false
    Value: System.InvalidOperationException: Nullable object must have a value.
>? Latitude.HasValue == true
Invalid operands in binary operator

The following evaluates to "true" even though HasValue is false:

         bool goodLat = Latitude.HasValue.Equals(true);
Comment 1 Jonathan Pobst 2012-01-13 18:26:41 UTC
This basically boils down to a deficiency in the debugger expression evaluator.

While paused, enter the following into the Immediate Window:

> true == true
Invalid operands in binary operator
Comment 2 scott 2012-01-13 23:34:18 UTC
OK - but single stepping in the debugger was demonstrating that I couldn't reliably evaluate HasValue, apart from the command window issue.  For instance, an expression like:
if((a.HasValue) && (b.HasValue)) 

(where a and b are both nullable doubles) would evaluate to true (and step into the 'if' block) even though both values were null.
Comment 3 Jeffrey Stedfast 2012-01-19 16:09:45 UTC
the "System.InvalidOperationException: Nullable object must have a value." looks to be coming from the runtime based on my debugging of the debugger today...

The last comment confuses me. Is "if((a.HasValue) && (b.HasValue))" evaluating incorrectly in the Immediate Window? Or is this in your source code? If it's in your source code, then that isn't a debugger issue, that's a compiler or runtime issue.

If it is in the Immediate window, I'll have to do more digging.
Comment 4 scott 2012-01-19 16:39:54 UTC
The problem appears in tooltips and in the command/immediate windows.  Nullable types that indeed do have a value report HasValue equal to false.  See the attached screenshot - you can see in the locals window that Latitude.HasValue reports false, whereas the result of evaluating "bool hasValue1 = Latitude.HasValue" shows hasValue1 equal to true.
Comment 5 scott 2012-01-19 16:40:37 UTC
Created attachment 1237 [details]
Shows behavior in debugger with nullable types
Comment 6 Jeffrey Stedfast 2012-01-19 16:53:36 UTC
Out of curiosity I've tested this out on Visual Studio for the ConsoleApplication project type and added some double? variables and inspected them in the debugger.

In the Immediate Window pane, here's what I get:

> a
> a.HasValue
> a.HasValue == true
> a.Value
A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

I've fixed the debugger code to display "null" for null Nullable types instead of "{}".

I also fixed our display of type names like "System.Nullable<double>" to display as "double?" which IMHO is nicer.
Comment 7 Jeffrey Stedfast 2012-01-19 17:09:44 UTC
Okay, so what you are saying is that in actual running live code, you have a "double? Latitude" and it's HasValue is true (because it actually has a value), but in the debugger (and immediate window), it displays HasValue as being false.

FWIW, I'm not actually able to reproduce this with MonoDevelop, so I suspect that either M4A needs to update their Mono.Debugger.Soft import or else it is some runtime issue in M4A?
Comment 8 Jeffrey Stedfast 2012-01-19 17:11:30 UTC
BTW, what version of Mono for Android is this? I assume 4.0?
Comment 9 scott 2012-01-19 17:38:29 UTC
4.0.1, yes.  Yes - your statement above is correct - using Visual Studio 2010 & m4a 4.0.1, I have a
"double? Latitude" and it's HasValue is true (because it actually has a value),
but in the debugger (and immediate window), it displays HasValue as being
Comment 10 Jonathan Pobst 2012-04-20 16:15:35 UTC
I've pulled in the latest debugging libraries for 4.1.1, and I do not see this, so hopefully it is fixed.

If you still see this on 4.1.1, please reopen!