views:

425

answers:

3

I have the following statement:

select new Action {
     ParentContentType = action.ParentContentType != null ? (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) : null 
};

ParentContentType is a nullable enum of type ContentType

action.ParentContentType maps to a database table which is a nullable int.

If action.ParentContentType isnt null, I determine the enum value using:

(ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)

In the case of when action.ParentContentType IS null I try to set the nullable enum to the value null.

This doesnt compile and I get:

Error   1 Type of conditional expression cannot be determined because there is no implicit conversion between ContentType' and '<null>'

EDIT

I could create a null enum value.. ie ContentType.EMPTY.

However:

ParentContentType = action.ParentContentType == null ? ContentType.EMPTY : (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType) };

Doesnt work either!

I get the exception:

The argument 'value' was the wrong type. Expected 'Enums.ContentType'. Actual 'System.Object'.
+1  A: 

I would go with your idea of ContentType.Null or ContentType.Empty otherwise you will be checking for nulls all throughout your application... Plus ContentType.Empty is more descriptive.

J.13.L
A: 

null is untyped. You have to cast it explicitly because the ? operator in C# requires that the second argument must be of the exact same type (or implicitly convertible) as the first.

Because the two must be of the same type, and null cannot be cast to a value type, they both must of the nullable type:

select new Action {
  ParentContentType = action.ParentContentType != null ?
    (ContentType?)Enum.ToObject(typeof(ContentType), action.ParentContentType) :
    (ContentType?)null 
};

However, this is pretty obscure. It never even occurred to me that you could create a nullable of an enum (I guess you can, since you posted the question -- I've never tried).

You will probably be better off with an enum value that means "nothing", as you suggested. That would be less surprising to most developers. You just don't expect an enum to be nullable.

Euro Micelli
Actually in this case casting the null will result in an exception " Could not translate expression"!
ListenToRick
That's weird. The above code compiled and ran for me. Can you post the definition of ParentContentType's type?
Euro Micelli
A: 

Oddly:

ParentContentType = action.ParentContentType == null ? ContentType.EMPTY : (ContentType)Enum.ToObject(typeof(ContentType), action.ParentContentType)

results in an exception:

The argument 'value' was the wrong type. Expected 'Enums.ContentType'. Actual 'System.Object'.

WTF?

ListenToRick
Well is ParentContentType an enum too? Then should it have a ContentType.Empty?
J.13.L
action.ParentContentType is a nullable int and is sourced from the DB
ListenToRick
However, the value in the db should never == null. DOh!
ListenToRick
The above compiled and ran for me as well. I don't know -- we must be missing something about the definition of the types.
Euro Micelli