473,493 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6158
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
14371
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
6010
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
8100
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
5
5637
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
640
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
35648
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
25
2995
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
3663
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
1
2460
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
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
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
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7195
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
7367
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...
1
4889
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...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.