22

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

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

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."

Now, how do I workaround it?

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
ldsenow
  • 5,885
  • 3
  • 21
  • 21
  • 1
    See http://stackoverflow.com/questions/6942803/why-c-sharp-decimals-cant-be-initialized-without-the-m-suffix – Slugart Nov 21 '13 at 13:59

4 Answers4

13

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

ldsenow
  • 5,885
  • 3
  • 21
  • 21
  • 3
    That is not a constant decimal value. *But* the DefaultValue-Attribute doesn't take a decimal as attribute, it just converts it later. Having an Attribute with an explicit decimal parameter it will throw again... – Oliver Friedrich Sep 20 '10 at 12:12
5

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.

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'.

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

[DefaultValue(typeof(Decimal), "440")]
public decimal TestValue { get; set; }
user461707
  • 51
  • 1
  • 1
  • I just ran into the same issue, in .Net 4.0. I needed to set DefaultValue to decimal.MaxValue. I'm not sure how Codeplex will wrap this, but it had to be written as: [DefaultValue(typeof(decimal), "79228162514264337593543950335")] public decimal StockLimit { get; set; } – midspace Mar 31 '16 at 10:04
4

You should place 440 inside quotation marks, like this:

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

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

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; }
        }
    }
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • It got compiled but anthoer error occurs when running the App The default value for the property 'paymentInAdvanceAmount' has different type than the one of the property itself – ldsenow Aug 06 '09 at 00:32
  • Perhaps you could post some code which shows us the problem then? – Ed S. Aug 06 '09 at 00:34
  • This won't help you for default values that are outside the range of Double, though. – Joey Aug 06 '09 at 00:37
  • 1
    Well, that was not the question, the question specifically said "440". – Ed S. Aug 06 '09 at 00:40
  • Aside from that point, none of the constructors even take a double, so it is a moot point. Don't see why that would get me a downvote... – Ed S. Aug 06 '09 at 00:43
  • Downvote is correct for that, because you do not enter a decimal. DefaultValue-Attribute doesn't take a decimal parameter in this case but a float maybe? It is converted though at runtime. – Oliver Friedrich Sep 20 '10 at 12:13