tags:

views:

829

answers:

7

Hi guys

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?

+1  A: 

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 Swangren
It got compiled but anthoer error occurs when running the AppThe default value for the property 'paymentInAdvanceAmount' has different type than the one of the property itself
ldsenow
I don't see how, the code above works just fine.
Ed Swangren
Perhaps you could post some code which shows us the problem then?
Ed Swangren
This won't help you for default values that are outside the range of Double, though.
Joey
Well, that was not the question, the question specifically said "440".
Ed Swangren
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 Swangren
*decimal*, not double
Ed Swangren
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.
BeowulfOF
That's true.[15chars]
Ed Swangren
A: 

My Code as below

public class ConfigSection : ConfigurationSection {

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

}

In my web app

protected void Page_Load(object sender, EventArgs e) { Response.Write(ConfigSection.PaymentInAdvanceAmount); }

with 440m it got compiled not successfully, with 440 or 440.00 it got compiled but another error will get thrown The default value for the property 'paymentInAdvanceAmount' has different type than the one of the property itself

ldsenow
These aren't answers, this is a response. Please either leave it as a comment on the answer you're replying to or, better yet, edit your question with this.
C. Ross
+3  A: 

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

ldsenow
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...
BeowulfOF
A: 

You should place 440 inside quotation marks, like this:

[ConfigurationProperty("paymentInAdvanceAmount", DefaultValue = "440")]
geoser
A: 

yes. have double quotation marks and it works.

Heinnge
A: 

[DefaultValue(typeof(Decimal), "440")]

public decimal TestValue { get; set; }

tphaneuf
A: 

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; }