i've got code intended convert null decimal vals "zero" value:
decimal paymenttot = tryconverttodecimal(boxpaymentamount.text); if ((null == paymenttot) || (paymenttot < 0)) { paymenttot = 0m; // or 0? } private decimal tryconverttodecimal(string incoming) { decimal result = 0.0m; try { result = convert.todecimal(incoming); } catch { ; // nada } return result; } it compiles, warning: "the result of expression 'false' since value of type 'decimal' never equal 'null' of type 'decimal?'"
i don't grok it's trying tell me. sort of test need appease warning emitter and, more importantly, see code can equate 'true' when intent?
decimal value type , can't null.
if want have nullable decimal use nullable<decimal> or decimal? wrapper on decimal type, (it still value type though).
you can have method parsing as:
private nullable<decimal> tryconverttodecimal(string incoming) { nullable<decimal> returnvalue = null; decimal result; if (decimal.tryparse(incoming, out result)) { returnvalue = result; } return returnvalue; } also better use decimal.tryparse if going ignore exception in parsing.
Comments
Post a Comment