473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

decimal.MaxValue should be a constant

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


Jul 21 '05 #1
4 6151
Günter Zöchbauer <zo****@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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.
"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Günter Zöchbauer <zo****@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Günter Zöchbauer <zo****@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP************************@msnews.microsoft.c om...
Günter Zöchbauer <zo****@gmx.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gustaf Liljegren | last post by:
I searched for previous answers on this, but couldn't find something fitting. I need advice on how to store decimal numbers with possible null values in memory. The numbers may be negative, so...
3
by: Typpo | last post by:
Hi, I have an ArrayList of decimals, and I want to figure out which two are the closest (least difference) from one another. I know I can loop through the decimals, but this is very tedious and...
10
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
5
by: Markus Kling | last post by:
"double.Parse(double.MaxValue.ToString())" yields the following Exception: Value was either too large or too small for a Double. at System.Number.ParseDouble(String value, NumberStyles options,...
4
by: Günter Zöchbauer | last post by:
can anyone tell why is valid but produces compiler error: An attribute argument must be a constant expression, typeof expression or array creation expression
6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
25
by: Lennart Benschop | last post by:
Python has had the Decimal data type for some time now. The Decimal data type is ideal for financial calculations. Using this data type would be more intuitive to computer novices than float as its...
26
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
1
by: Joey Fontaine | last post by:
I just noticed that, when using intellisense, the DateTime.MaxValue field has a static property icon whereas the Decimal.MaxValue field has a constant property icon. However, when looking at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.