无法应用于操作数';字符串';和';decimal';

本文关键字:decimal 字符串 应用于 操作数 | 更新日期: 2023-09-27 18:28:40

我目前正在编写一些测试,以查看Dictionary中的值是否匹配。例如"TestOperatorMatchNotFoundIfValueNotNumeric"等。我已经成功地在将字符串与字符串进行比较时编写了测试,但现在我必须检查该值是否不是数字(使其无效)

我曾尝试使用以前测试中使用的逻辑进行一些调整,但我收到错误消息"运算符"=="无法应用于类型为"string"answers"decimal"的操作数。"。下面是我正在处理的代码,其中包含第8行和第9行的两条错误消息。非常感谢您的帮助。

 public bool IsMatch(Dictionary<String, String> variableData)
    {
        decimal outputValue;
        bool isANumber = Decimal.TryParse("StringValue", out outputValue);
        if (variableData.ContainsKey(this.value1Name))
        {
            if (comparisonOperator == "==")
            {
                if (variableData[this.value1Name] == this.value2Literal) //Error Msg
                {
                    return true;
                }
            }
            else
            {
                if (variableData[this.value1Name] != this.value2Literal) //Error Msg
                {
                    return true;
                }
            }
        }
        return false;
    }

无法应用于操作数';字符串';和';decimal';

您不能比较1.00=="1.00",您可以转换为十进制("1.00")并进行比较。但我不后悔。

你看你的字典有字符串值。

所以你的代码会变成

Convert.ToDecimal(variableData[this.value1Name]) == this.value2Literal