473,546 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# const declaration doc error

In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?
Nov 15 '05 #1
20 3537
What do you mean the compiler generates a static modifier? If you are
talking about what IL is produced, then you can't apply what is in the C#
language spec to what you see in the IL, because that follows a different
spec. Ultimately, a constant is represented as a static variable. It is a
C# construct only, and has nothing to do how it is represented in IL.

Hope this helps.

"Blah" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?

Nov 15 '05 #2
Hi Blah,

"Blah" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?


Constants are implicitly static. Since they can only be one value
there's no reason to make them instance members and being static makes them
more flexible (can reference without an instance of the class). Are you sure
you can specify "static const"? For me the compiler (VS 2003) detects an
error as expected ("The constant 'MyClass.MY_CON STANT' cannot be marked
static").

Regards,
Dan
Nov 15 '05 #3
100
Hi Blah,
Actually constants are only a meta data. There is no any fields behind them.
You can see that so called field has attribute *literal* applied.
Nicholas is right. The way you declare and use constants in c# is according
the c# spec and has nothing to do with IL and CLR.

B\rgds
100

"Blah" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?

Nov 15 '05 #4
The documentation states that its not possible to declare them static, when
infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do. and also the compiler itself (if you
check object browser) states that they are what the documentation says they
are Not.
My point is, its confusing and inaccurate in the documentation.
"100" <10*@100.com> wrote in message
news:Op******** ******@tk2msftn gp13.phx.gbl...
Hi Blah,
Actually constants are only a meta data. There is no any fields behind them. You can see that so called field has attribute *literal* applied.
Nicholas is right. The way you declare and use constants in c# is according the c# spec and has nothing to do with IL and CLR.

B\rgds
100

"Blah" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?


Nov 15 '05 #5
What is the benifit of specifying a const in C# as static then? What is the
difference between a static const (which MSDN says clearly that cannot be
done but it can) and a non static const.

Anybody care to enlighten me as MSDN does a good job of confusing the issue.
"100" <10*@100.com> wrote in message
news:Op******** ******@tk2msftn gp13.phx.gbl...
Hi Blah,
Actually constants are only a meta data. There is no any fields behind them. You can see that so called field has attribute *literal* applied.
Nicholas is right. The way you declare and use constants in c# is according the c# spec and has nothing to do with IL and CLR.

B\rgds
100

"Blah" <an*******@disc ussions.microso ft.com> wrote in message
news:0f******** *************** *****@phx.gbl.. .
In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?


Nov 15 '05 #6
100
Hi,
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error.


What do you mean? My C# compiler emits an error if I try to put *static*
modifier in constant declaration.

B\rgds
100
Nov 15 '05 #7
100 <10*@100.com> wrote:
Actually constants are only a meta data. There is no any fields behind them.


In what way? I can't see anything in any spec saying there aren't any
fields behind them, and reflection can certainly get them as fields:

using System;
using System.Reflecti on;

public class Test
{
public const int x = 5;

static void Main()
{
foreach (FieldInfo fi in typeof(Test).Ge tFields())
{
Console.WriteLi ne ("{0}={1}", fi.Name, fi.GetValue(nul l));
}
}
}

Yes, references to them are inlined, but I don't think that means
they're not fields.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
bwahahahaha <bw*******@bwah ahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when
infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
bwahahahaha <bw*******@bwah ahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

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

Similar topics

2
3623
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
7
4349
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
16
41747
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the...
0
1864
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you...
4
6675
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
9
1786
by: Ben Voigt | last post by:
Found another variation on my previous bug (https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100917) I would welcome validation and votes. class ISkewEstimator {
9
10508
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
4
1907
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
Suppose I have a function that takes a pointer as its input, but does not change what the pointer points to. In that case, the const qualifier can be used to indicate that the variable pointed to is not changed. But should this const qualifier be used in the function declaration, the function definition, or both? Example: In .h-file...
7
2070
by: W Marsh | last post by:
Hi, Could anybody tell me wh the parameter "T val" is not marked const in this Stroustrup code, considering that val is not modified and not non- const methods called? template<class C, class Tint count(const C&v, T val) { typename C::const_iterator i = find(v.begin(), v.end(), val); int n = 0;
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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 we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.