简体   繁体   English

如何测试十进制为空?

[英]How can I test a decimal for null?

I've got this code intended to convert null decimal vals to a "zero" value: 我得到的这段代码旨在将空十进制值转换为“零”值:

decimal paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if ((null == paymentTot) || (paymentTot < 0))
{
    paymentTot = 0M; // or simply 0?
}

private decimal TryConvertToDecimal(String incoming)
{
    decimal result = 0.0M;
    try
    {
        result = Convert.ToDecimal(incoming);
    }
    catch
    {
        ; // nada
    }
    return result;
}

It compiles, but I get this warning: "The result of the expression is always 'false' since a value of type 'decimal' is never equal to 'null' of type 'decimal?'" 它可以编译,但是我得到警告:“表达式的结果始终为'false',因为类型'decimal'的值永远不会等于'decimal类型的'null'?”

I don't grok just what it's trying to tell me. 我不只是想告诉我什么。 What sort of test do I need to appease the warning emitter and, more importantly, see to it that my code can equate to 'true' when that is the intent? 我需要什么样的测试来安抚警告发射器,更重要的是,在这样做的时候,确保我的代码可以等同于“ true”?

decimal is a value type and it can't be null. decimal是一种值类型,不能为null。

If you want to have a nullable decimal then use Nullable<decimal> or decimal? 如果要使用Nullable<decimal>为空的十进制,请使用Nullable<decimal>还是decimal? which is a wrapper on decimal type, (it is still a value type though) . 这是decimal类型的包装器(尽管它仍然是值类型)

See: Nullable<T> Structure 请参见: Nullable<T> Structure

You can also have your method for 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 it is better to use decimal.TryParse if you are going to ignore the exception in parsing. 另外 ,如果要在解析中忽略异常,则最好使用decimal.TryParse

You possibly want to return a Nullable<decimal> (shorthand decimal? ) from TryConvertToDecimal because decimal is a non-nullable value type. 您可能想从TryConvertToDecimal返回Nullable <decimal> (速写decimal? ),因为十进制是一种不可为空的值类型。

private decimal? TryConvertToDecimal(String incoming)
{
    try
    {
        return Convert.ToDecimal(incoming);
    }
    catch
    {
       return null;
    }
}

var paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if (!paymentTot.HasValue || paymentTot.Value < 0)
{
    paymentTot = 0;
}

Note however that by convention TryXXX functions return bool and use an out argument to return the parsed value upon success. 但是请注意,按照惯例, TryXXX函数将返回bool并在成功时使用out参数返回已解析的值。 One of these built in to the Framework is decimal.TryParse which you might want to use instead: 框架中内置的其中之一是decimal.TryParse ,您可能想要使用它:

decimal paymentTot;
if(!decimal.TryParse(boxPaymentAmount.Text, out paymentTot) || paymentTot < 0)
    paymentTot = 0;

will default to 0 if the parsing fails or if the parsed value is < 0, as specified in your question. 如果解析失败您的问题中指定的解析值<0,则默认为0。 I would suggest this is most likely the solution you are looking for. 我建议这很可能是您正在寻找的解决方案。

You don't Need your own Converter for this, use this instead: 您不需要为此使用自己的Converter,而是使用以下代码:

decimal paymentTot;
if(!decimal.TryParse(boxPaymentAmount.Text, out paymentTot))
    paymentTot = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM