C# 如何设置常量十进制值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1236402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:02:11  来源:igfitidea点击:

How to set a constant decimal value

c#decimal

提问by ldsenow

I'm using C# to set a default value for a decimal value in my config class

我正在使用 C# 为我的配置类中的十进制值设置默认值

public class ConfigSection : ConfigurationSection
{
        [ConfigurationProperty("paymentInAdvanceAmount", **DefaultValue = 440m**)]
        public decimal PaymentInAdvanceAmount
        {
            get { return (decimal)base["paymentInAdvanceAmount"]; }
            set { base["paymentInAdvanceAmount"] = value; }
        }
}

but it won't be compiled and throws an error

但它不会被编译并抛出错误

An attribute argument must be a constant expression, typeof expression

属性参数必须是常量表达式,typeof 表达式

I found a post says: "It's not a bug. "1000M" is merely shorthand for "new Decimal(1000)", which involves a method call, which means it's not considered a constant. Just because the compile lets you pretend it's a constant most of the time, doesn't mean you can all of the time."

我发现一个帖子说:“这不是一个错误。“1000M”只是“new Decimal(1000)”的简写,它涉及一个方法调用,这意味着它不被认为是一个常量。仅仅因为编译让你假装它是一个大部分时间保持不变,并不意味着你可以一直保持。”

Now, how do I workaround it?

现在,我该如何解决它?

回答by Ed S.

Just use 440 and leave out the 'M'. I get no compilation errors, and this program works as expected:

只需使用 440 并省略“M”。我没有遇到编译错误,这个程序按预期工作:

namespace WindowsApplication5
{
    public partial class Form1 : Form
    {
        public Form1( )
        {
            InitializeComponent( );
            AttributeCollection attributes = 
                TypeDescriptor.GetProperties( mTextBox1 )[ "Foo" ].Attributes;           
            DefaultValueAttribute myAttribute =
               ( DefaultValueAttribute ) attributes[ typeof( DefaultValueAttribute ) ];

            // prints "440.1"
            MessageBox.Show( "The default value is: " + myAttribute.Value.ToString( ) );
        }
    }

    class mTextBox : TextBox
    {
        private decimal foo;       
        [System.ComponentModel.DefaultValue( 440.1 )]
        public decimal Foo
        {
            get { return foo; }
            set { foo = value; }
        }
    }
}

回答by ldsenow

I finally found out it I enter "440" instead of 440m or 440. It got compiled and runs well

我终于发现我输入了“440”而不是 440m 或 440。它被编译并且运行良好

回答by ldsenow

You should place 440 inside quotation marks, like this:

您应该将 440 放在引号内,如下所示:

[ConfigurationProperty("paymentInAdvanceAmount", DefaultValue = "440")]

回答by user461707

I found that if you set a default value for a decimal property and specified that value in quotes, it did not work for me with a WinForms control and .NET 3.5.

我发现如果您为小数属性设置默认值并在引号中指定该值,则它不适用于 WinForms 控件和 .NET 3.5。

When ever I right clicked on the property in the designer "Properties" window and selected the "Reset" option I got the message "Object of type 'System.String' cannot be converted to type 'System.Decimal'.

当我右键单击设计器“属性”窗口中的属性并选择“重置”选项时,我收到消息“'System.String'类型的对象无法转换为'System.Decimal'类型。

To get it to work I had to use the same code as tphaneuf suggested i.e.

为了让它工作,我必须使用与 tphaneuf 建议的相同的代码,即

[DefaultValue(typeof(Decimal), "440")]
public decimal TestValue { get; set; }