C# 可空对象必须有值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10555682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nullable object must have a value?
提问by Sealer_05
On the line: bool travel = fill.travel.Value;I am getting the following error:
在线:bool travel = fill.travel.Value;我收到以下错误:
Nullable object must have a value
可为空的对象必须有值
and i am not sure why. All I want to do is get the value in the database of travel which is currently false. Any help would be appreciated.
我不知道为什么。我想要做的就是获取当前为 false 的旅行数据库中的值。任何帮助,将不胜感激。
using (var db = new DataClasses1DataContext())
{
var fill = (from f in db.expenseHdrs
where f.rptNo == getPkRowReport()
select f).FirstOrDefault();
txtReportDesc.Text = fill.description;
txtPeriod.Text = fill.period;
txtPurpose.Text = fill.purpose;
bool travel = fill.travel.Value;
chkTravel.Checked = travel
}
采纳答案by Tejs
You can always switch to
您可以随时切换到
fill.travel.GetValueOrDefault()
To provide the default (false), or the value of the boolean column from the database. Or you can specify the default with an overload. Either way, the nullable currently doesnt have a value, which is why you get that exception.
提供默认值 (false) 或数据库中布尔列的值。或者您可以使用重载指定默认值。无论哪种方式,可空对象当前都没有值,这就是您收到该异常的原因。
回答by Peter Ritchie
You will get a InvalidOperationExceptionif you access the Nullable.Value property when the HasValue property is false.
InvalidOperationException如果您在 HasValue 属性为 false 时访问 Nullable.Value 属性,您将获得一个。
回答by Adil
You can check if nullable variable has some value like this before your actually access its value
您可以在实际访问其值之前检查可空变量是否具有这样的值
if(fill.travel.HasValue)
{
bool travel = fill.travel.Value;
}
回答by Oblivion2000
The value coming from the database is a nullable boolean. When you call Nullable.Value, and the value is null, you will get this exception. Consider checking the property Nullable.HasValue before calling .Value.
来自数据库的值是一个可为空的布尔值。当您调用 Nullable.Value 并且值为 null 时,您将收到此异常。考虑在调用 .Value 之前检查属性 Nullable.HasValue。
回答by Val Bakhtin
You are trying to access property from the nonexistent object (your fill.travel is null, and you calling prop from it), you can use coalesce operator (.Net 4.0):
您正在尝试从不存在的对象访问属性(您的 fill.travel 为空,并且您从中调用 prop),您可以使用合并运算符(.Net 4.0):
bool travel = fill.travel ?? false;
回答by StarPilot
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
空不是假的。请参阅 Eric Lippert 的关于此的博客文章系列: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.
您需要将 null 结果处理为 false,如果这是您想要的翻译方式。
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
或者,如果您使用的是 .Net 4,请使用 Val Bakhtin 的解决方案
回答by Cornelius J. van Dyk
You can always use the classic C ? operator like this:
你可以一直使用经典的 C 吗?像这样的运算符:
bool travel = (fill.travel == null ? false : fill.travel.Value);
回答by How 'bout a Fresca
I had this happen because there was code like this:
我发生这种情况是因为有这样的代码:
bool? someVariable = (bool)someNullableBool;
The someNullableBool was null and this error was thrown.
someNullableBool 为空并且抛出了这个错误。
The solution was to re-write it as:
解决方案是将其重写为:
bool? someVariable = (bool?)someNullableBool;

