Nullable Enum nullable type question

Posted by Michael Kniskern on Stack Overflow See other posts from Stack Overflow or by Michael Kniskern
Published on 2009-01-08T23:36:55Z Indexed on 2010/04/07 2:43 UTC
Read the original article Hit count: 472

Filed under:
|
|

I get the following compilation error with the following source code:

Compilation Error:

Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'MyEnum'

Source Code

public enum MyEnum
{
    Value1, Value2, Value3
}

public class MyClass
{
    public MyClass() {}
    public MyEnum? MyClassEnum { get; set; }
}

public class Main()
{
   object x = new object();

   MyClass mc = new MyClass()
   {
        MyClassEnum = Convert.IsDBNull(x) : null ? 
            (MyEnum) Enum.Parse(typeof(MyEnum), x.ToString(), true)
   };
}

How can I resolve this error?

© Stack Overflow or respective owner

Related posts about c#2.0

Related posts about nullable-types