Nullable property values

Nullable property values

Old forum URL: forums.lhotka.net/forums/t/5562.aspx


TJones posted on Thursday, October 09, 2008

Using cslacs-3.5.0-080803, I have created a simple EditableRoot object that expects nulls from the stored procedure, but we'd hoped not to have to change existing code that does not expect nulls from the objects.  Therefore the property is declared as follows:

private static PropertyInfo<long> myIDProperty = RegisterProperty<long>(typeof(myClass), new PropertyInfo<long>("myID"));

But the following code in DataPortal_Fetch() throws an exception whenever it encounters a null.

LoadProperty<long, long?>(myIDProperty , data.myID);

I traced this problem to the CoerceValue() method which has a host of checks for unsupported type conversions, but nothing for this particular situation (e.g. long? to long).

Is this an issue that has been or will be corrected, or should I expect to always expect nullable properties from my objects if they are returned as nulls from the database?

TJ

rsbaker0 replied on Friday, October 10, 2008

I don't have much experience with the new managed properties in CSLA 3.5, but if data.myID is nullable, maybe you could do...

LoadProperty<long, long>(myIDProperty , data.myID.GetValueOrDefault(...default value here...));

TJones replied on Friday, October 10, 2008

Actually, adding .GetValueOrDefault(...default value here...)  seems to work even without changing long? to long. This fixes the problem for now, but seems to be a work around.  I'll add this to my template code.  Thanks.

Copyright (c) Marimer LLC