Nullable object must have a value?

后端 未结 8 2379
旧时难觅i
旧时难觅i 2020-12-09 14:54

On the line: bool travel = fill.travel.Value; I am getting the following error:

Nullable object must have a value

a

8条回答
  •  不思量自难忘°
    2020-12-09 15:01

    Null is not false. See Eric Lippert's blog article series about this at: http://blogs.msdn.com/b/ericlippert/archive/2012/03/26/null-is-not-false.aspx

    You need to process a null result into a false, if that is how you want to translate it.

    To make this case clear, consider this code:

    bool travel;
    bool? temptravel = fill.travel.Value;
    if( temptravel == true )
        travel = true;
    else
        travel = false;
    

    Or just use Val Bakhtin's solution if you are using .Net 4

提交回复
热议问题