Type Converters and Null

Posts   
 
    
alexk
User
Posts: 19
Joined: 14-Sep-2005
# Posted on: 09-Nov-2005 23:24:50   

I'm using a type converter to convert values from a Sql Server int column to instances of a custom C# type.

In the type converter's "ConvertFrom()", I have


if (value.GetType() == typeof(int))
    return SomeClass.Parse((int) value);
else if (value.GetType() == typeof(DBNull))
    return null;
else
...

In the "ConvertTo()", I have


if (value == null)
    return DBNull.Value;

The section that is causing the exception is in an entity's get accessor that returns the custom type.


if(valueToReturn == null)
    {
        valueToReturn = TypeDefaultValue.GetDefaultValue(typeof(SomeClass));
    }
return (SomeClass)valueToReturn;

However, GetDefaultValue() is returning "new object()" because "defaultValueType.UnderlyingSystemType.FullName" isn't any of the cases listed. The resulting "(SomeClass)valueToReturn" downcast fails.

In response to this exception, I initially changed my type converter to return a "default" value of my custom type when ConvertFrom() gets a DBNull.Value (like "0" with Int32). Since my type is a reference type though, null is more natural, and I'd like to use that.

Returning null instead of "new object()" in "GetDefaultValue" would fix this particular issue. However, I don't know what side effects that would have. Also, there would be the usual concerns with backwards compatibility.

Is the only solution to go back to a "default" value to represent database nulls? Thanks!

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 10-Nov-2005 02:26:24   

What is the purpose of SomeClass. Are you wanting null int values to be seen as DBNull. You would have SomeClass have a property like _isnull, so your default constructor should set that value to true and then when you return the default SomeClass you can check your object to see if it is null and proceed if not retrieve the int value from the object. I may be missing the question, but I would prefer to return a SomeClass object set with _isnull to true over returning null, that way your cast to some class would work also.

alexk
User
Posts: 19
Joined: 14-Sep-2005
# Posted on: 10-Nov-2005 06:47:54   

I would prefer to return a SomeClass object set with _isnull to true over returning null, that way your cast to some class would work also.

Casting a null value to any reference type is valid and just results in null.

Are you wanting null int values to be seen as DBNull.

My explanation was poor. The problem stems from the int database column being nullable. When a row has a non-null value for that column, the value is converted to an instance of SomeClass. However, when the database value is null, I'd like null to be returned by the type converter. That is what the code snippet from my implementation of ConvertFrom() is doing. That part is okay but later the null value causes a problem with the code gen'ed get accessor for a property of type SomeClass.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39882
Joined: 17-Aug-2003
# Posted on: 10-Nov-2005 09:29:38   

TypeDefaultValue.cs is generated by a template which uses an include template you can adjust. The template is called typeDefaultValueInclude.template and is located in SharedTemplates\C# (or VB.NET).

Create a copy of that template, then add your types to the list of cases. Then create a copy of the templateset config you're using and change the template ID binding so it uses the copy of the template instead (i.e. your filename instead of typeDefaultValueInclude.template). Regenerate the code et viola.

Frans Bouma | Lead developer LLBLGen Pro