vb.net - If - Strange Behavior in Detecting Type -
i discovered strange behavior in vb.net today in trying work nullable datetime data. pulling datetime value out of xml file inserting database, , want allow empty value. thought should use if
prevent casting errors:
dim lastrun datetime? = _ if(rowdata("lastrun") = "", nothing, ctype(rowdata("lastrun"), datetime))
it seems should return value of nothing
in case if
false, or value of date time lastrun
if value not blank. instead, when if
condition returns false, value of datetime.minvalue
, causes exception on insert database due sql datetime underflow.
i able fix using datetime?
cast in last parameter, behavior seems odd me. expected type datetime?
because that's variable type. also, narrowest type can allow both possible result values datetime?
, since either datetime
or nothing
. , yet somehow decides result value should datetime
, guess typecasts nothing
datetime.minvalue
? going on here?
part of problem i'm used c#, , equivalent expression rowdata["lastrun"] == "" ? null : (datetime)rowdata["lastrun"])
doesn't compile (as expected), because there's "no implicit conversion between datetime
, null
."
nothing
not same null
in c#, mixture between null
, default(t)
. when use nothing
on value type(like structure datetime
) it's default value datetime.minvalue
.
Comments
Post a Comment