本文介绍了比较问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图这样做: public static void GetValueFromDbObject(object dbObjectValue,ref decimal destination) { destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue: (十进制)dbObjectValue; } 我只想说dbObjectValue是否相等到一个 DBNull.Value或",make destination = decimal.MaxValue。 这给了我: nullHandler.cs(24,18):错误CS0019:运算符''||''不能应用于 类型为''bool''和''string''的操作数br /> 如何更改该语句以执行我要执行的操作? 我试图将传递的值(dbObjectValue)移动到目的地, 除非=空或空字符串(因为这可能来自空白文本框)。 谢谢, Tom I tried to do this: public static void GetValueFromDbObject(object dbObjectValue, ref decimaldestination){destination = dbObjectValue == DBNull.Value || "" ? decimal.MaxValue :(decimal)dbObjectValue;} Where I am just trying to say if dbObjectValue is either equal to aDBNull.Value or "", make destination = decimal.MaxValue. This gives me: nullHandler.cs(24,18): error CS0019: Operator ''||'' cannot be applied tooperands of type ''bool'' and ''string'' How would I change that statement to do what I am trying to do? I am trying to move the value passed (dbObjectValue) into destination,unless = Null or empty string (as this may come from a blank textbox). Thanks, Tom推荐答案 问题在于评估顺序。 C#正在评估此部分 首先: dbObjectValue == DBNull.Value 这导致一个布尔值表达式,不能与 字符串进行比较。要使?:运算符工作,您需要比较两个类似 类型。这应该有效: destination =((dbObjectValue == DBNull.Value)|| (dbObjectValue == string.Empty))? decimal.MaxValue: (十进制)dbObjectValue; HTH! Mike The problem is in the order of evaluation. C# is evaluating this partfirst: dbObjectValue == DBNull.Value This results in a boolean expression, which can''t be compared to astring. To make the ?: operator work you need to compare two liketypes. This should work: destination = ((dbObjectValue == DBNull.Value) ||(dbObjectValue == string.Empty)) ?decimal.MaxValue :(decimal)dbObjectValue; HTH!Mike 这篇关于比较问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 02:16