decimal.MaxValue should be a constant

  • Thread starter Thread starter Günter Zöchbauer
  • Start date Start date
G

Günter Zöchbauer

can anyone tell why

[DefaultValue(int.MaxValue)] is valid

but

[DefaultValue(decimal.MaxValue)] produces compiler error:

An attribute argument must be a constant expression, typeof expression or
array creation expression
 
Günter Zöchbauer said:
can anyone tell why

[DefaultValue(int.MaxValue)] is valid

but

[DefaultValue(decimal.MaxValue)] produces compiler error:

An attribute argument must be a constant expression, typeof expression or
array creation expression

It looks like it's a bug in the way decimal.MaxValue is declared.
Rather than a compile-time constant, it's declared to just be static
readonly. I suspect this may make it easier to "get right" in the
class, but it's certainly a bit of a downside for anyone wanting to use
it in the way you do.
 
Thanks for your help.

I tried a simple workaround but got the same error.

private const decimal maxValueDefault = 2^96;
[DefaultValue(maxValueDefault)]

Looks like the compiler can't handle decimal as constant.


Günter Zöchbauer said:
can anyone tell why

[DefaultValue(int.MaxValue)] is valid

but

[DefaultValue(decimal.MaxValue)] produces compiler error:

An attribute argument must be a constant expression, typeof expression or
array creation expression

It looks like it's a bug in the way decimal.MaxValue is declared.
Rather than a compile-time constant, it's declared to just be static
readonly. I suspect this may make it easier to "get right" in the
class, but it's certainly a bit of a downside for anyone wanting to use
it in the way you do.
 
Günter Zöchbauer said:
Thanks for your help.

I tried a simple workaround but got the same error.

private const decimal maxValueDefault = 2^96;
[DefaultValue(maxValueDefault)]

Looks like the compiler can't handle decimal as constant.

Well, the first part compiles, so the compiler can certainly handle
decimal as a constant. What it can't do is apply it to attribute
parameters, as they can only be of types bool, byte, char, double,
float, int, long, short, or string. (C# type names copied from the spec
- hopefully it's obvious to any VB.NET readers what they correspond
to.)

So it looks like in this case it's the compiler error message which is
dodgy, not the compiler rules themselves.
 
Thanks again.
I was not aware of the fact that the types allowed as attribute parameter
are limited.
ShouldSerializeXXX and ResetXXX are the solution in this case.

Günter Zöchbauer said:
Thanks for your help.

I tried a simple workaround but got the same error.

private const decimal maxValueDefault = 2^96;
[DefaultValue(maxValueDefault)]

Looks like the compiler can't handle decimal as constant.

Well, the first part compiles, so the compiler can certainly handle
decimal as a constant. What it can't do is apply it to attribute
parameters, as they can only be of types bool, byte, char, double,
float, int, long, short, or string. (C# type names copied from the spec
- hopefully it's obvious to any VB.NET readers what they correspond
to.)

So it looks like in this case it's the compiler error message which is
dodgy, not the compiler rules themselves.
 
Back
Top