Bug 4198 - Nullable Property wrapper for XElement not setting HasValue correctly
Summary: Nullable Property wrapper for XElement not setting HasValue correctly
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-02 17:49 UTC by kenneth.beasley
Modified: 2012-04-03 17:01 UTC (History)
2 users (show)

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

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 or GitHub 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:
Status:
RESOLVED FIXED

Description kenneth.beasley 2012-04-02 17:49:21 UTC
I have a nullable decimal property which wraps an XElement value.  The setter uses XElement's SetElementValue.

When I try setting this property to a decimal, the HasValue property does not turn to true.  When I look at the nonpublic members of the nullable decimal, the value was set correctly, but has_value is still false.  

This makes the public value exhibit a System.InvalidOperation Exception: Nullable object must have a value, likely because the public HasValue is equal false.

Somewhere, has_value isn't getting set to true when it should be, when using SetElementValue, causing the property to be nonfunctional.
Comment 1 Eric Beisecker 2012-04-03 13:41:59 UTC
Hello Kenneth,

Based on your description is this code sample similar to yours? It seems to exhibit the correct behavior for me. 

XElement element = new XElement("TestNode");

public decimal? DecimalProperty
{
    get
    {
        if(!string.IsNullOrEmpty(element.Value))
            return Convert.ToDecimal(element.Value);

        return null;
    }

    set
    {
        element.SetElementValue("TestNode", value);
    }
} 

May I also ask what version of Mono for Android you're using and if you're using Visual Studio or MonoDevelop.

Thanks.
Comment 2 kenneth.beasley 2012-04-03 17:01:49 UTC
It appears in this case, the debugger was giving me false information.  Indeed, grabbing the element value afterwards and putting it in a variable works.

I've found what the actual bug was, but I'll put that in another ticket.

For reference, using the latest 4.06 Mono for Android targeted at Android 2.2 and using Visual Studio 2010