我从数据库获取答案并需要检查它是否为null
,然后再将其放入值中。
我尝试了几种检查的变体,但我的代码仍然崩溃:
foreach(i, point;myPointsLonLat)
{
try
{
carGPSPoint cargpspoint;
cargpspoint.id = point[0].coerce!ulong;
cargpspoint.recordDate = DateTime.fromSimpleString(point[1].coerce!string).toISOExtString(); // some magic to get string in 2016-10-31T15:37:24 format
if(point[2].hasValue) //check if we have some data
cargpspoint.velocity = point[2].coerce!double;
if(point[3].hasValue)
cargpspoint.lat = point[3].coerce!double;
if(point[4].hasValue)
cargpspoint.lon = point[4].coerce!double;
cargpspoints[i] = cargpspoint;
b.next();
}
catch(Exception e)
{
writeln("\n----------------------------ERRRRRR-----------------------");
writeln(point);
writeln("1111: ", point[2].coerce!double);
writeln("2222: ", point[3].coerce!double);
writeln("3333: ", point[4].coerce!double);
writeln(e.msg);
}
}
输出:
----------------------------ERRRRRR-----------------------
Row([1478698195002489886, 2016-Nov-09 13:29:55, 153, null, null], [false, false, false, true, true])
1111: 153
object.Exception@C:\D\dmd2\windows\bin\..\..\src\phobos\std\variant.d(823): Type typeof(null) does not convert to double
你可以看到它打印出了1111: 153
。但在其他空变量上会崩溃。
我也尝试过:
if(point[2].type !is null) //check if we have some data
cargpspoint.velocity = point[2].coerce!double;
if(point[3].type !is null)
cargpspoint.lat = point[3].coerce!double;
if(point[4].type !is null)
cargpspoint.lon = point[4].coerce!double;
同样的结果。我做错了什么吗?
.isNull
或其他方法。或者至少.hasValue
在null
上返回false。似乎在C#中是这样做的。如果我错了,请纠正我。 - Dmitry Bubnenkov