c# - Unable to ChangeType from string to int? -
this question has answer here:
in depths of our application there attempt perform conversion string nullable int convert.changetype(value, casttype)
. in case values follows:
value: "00010" casttype: nullable<system.int16>
the issue receiving following error
invalid cast 'system.string' 'system.nullable`1[[system.int16}
i had (obviously incorrectly) believed similar cast or convert.toint16() i've verified it's not same testing following 2 lines of code.
int16 t = convert.toint16("00010"); object w = convert.changetype("00010", typeof(short?));
as might suspect, first succeeds whereas second fails error message above.
is possible use changetype in fashion making adjustment or should @ refactor?
you'll need write this:
var value = "00010"; short? w = value == null ? null : (short?)convert.changetype("00010", typeof(short));
changetype
not work nullable<t>
- , if have value value
, assume have value converted type
Comments
Post a Comment