C# const declaration doc error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blah

    #1

    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?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: C# const declaration doc error

    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" <anonymous@disc ussions.microso ft.com> wrote in message
    news:0fed01c393 db$b90fd7d0$a10 1280a@phx.gbl.. .[color=blue]
    > 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?
    >
    >[/color]


    Comment

    • Daniel Pratt

      #3
      Re: C# const declaration doc error

      Hi Blah,

      "Blah" <anonymous@disc ussions.microso ft.com> wrote in message
      news:0fed01c393 db$b90fd7d0$a10 1280a@phx.gbl.. .[color=blue]
      > 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?[/color]

      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


      Comment

      • 100

        #4
        Re: C# const declaration doc error

        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" <anonymous@disc ussions.microso ft.com> wrote in message
        news:0fed01c393 db$b90fd7d0$a10 1280a@phx.gbl.. .[color=blue]
        > 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?
        >
        >[/color]


        Comment

        • bwahahahaha

          #5
          Re: C# const declaration doc error

          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" <100@100.com> wrote in message
          news:OpDsIs$kDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
          > Hi Blah,
          > Actually constants are only a meta data. There is no any fields behind[/color]
          them.[color=blue]
          > You can see that so called field has attribute *literal* applied.
          > Nicholas is right. The way you declare and use constants in c# is[/color]
          according[color=blue]
          > the c# spec and has nothing to do with IL and CLR.
          >
          > B\rgds
          > 100
          >
          > "Blah" <anonymous@disc ussions.microso ft.com> wrote in message
          > news:0fed01c393 db$b90fd7d0$a10 1280a@phx.gbl.. .[color=green]
          > > 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?
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • bwahahahaha

            #6
            Re: C# const declaration doc error

            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" <100@100.com> wrote in message
            news:OpDsIs$kDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
            > Hi Blah,
            > Actually constants are only a meta data. There is no any fields behind[/color]
            them.[color=blue]
            > You can see that so called field has attribute *literal* applied.
            > Nicholas is right. The way you declare and use constants in c# is[/color]
            according[color=blue]
            > the c# spec and has nothing to do with IL and CLR.
            >
            > B\rgds
            > 100
            >
            > "Blah" <anonymous@disc ussions.microso ft.com> wrote in message
            > news:0fed01c393 db$b90fd7d0$a10 1280a@phx.gbl.. .[color=green]
            > > 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?
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • 100

              #7
              Re: C# const declaration doc error

              Hi,[color=blue]
              > The documentation states that its not possible to declare them static,[/color]
              when[color=blue]
              > infact i was permited to, its a doc error.[/color]

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

              B\rgds
              100


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: C# const declaration doc error

                100 <100@100.com> wrote:[color=blue]
                > Actually constants are only a meta data. There is no any fields behind them.[/color]

                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 - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: C# const declaration doc error

                  bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=blue]
                  > 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.[/color]

                  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 - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • bwahahahaha

                    #10
                    Re: C# const declaration doc error

                    Mine builds ok on 2003. Bloody odd.

                    public static const int WM_COMMAND = 0x112;

                    Works for me.


                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.19f8d5 7b28af91279898a 7@msnews.micros oft.com...[color=blue]
                    > bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=green]
                    > > The documentation states that its not possible to declare them static,[/color][/color]
                    when[color=blue][color=green]
                    > > 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.[/color]
                    >
                    > 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 - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • bwahahahaha

                      #11
                      Re: C# const declaration doc error

                      try VS.NET 2003.


                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.19f8d5 7b28af91279898a 7@msnews.micros oft.com...[color=blue]
                      > bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=green]
                      > > The documentation states that its not possible to declare them static,[/color][/color]
                      when[color=blue][color=green]
                      > > 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.[/color]
                      >
                      > 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 - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: C# const declaration doc error

                        bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=blue]
                        > Mine builds ok on 2003. Bloody odd.
                        >
                        > public static const int WM_COMMAND = 0x112;
                        >
                        > Works for me.[/color]

                        Doesn't work for me with VS.NET 2003. Is it definitely, definitely
                        building that file? Quick way to check: if you change it to

                        public xxx const int WM_COMMAND = 0x112;

                        does it *then* give a compiler error?

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                        If replying to the group, please do not mail me too

                        Comment

                        • bwahahahaha

                          #13
                          Re: C# const declaration doc error

                          IT worked for me today, strange.


                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          news:MPG.19f8db ba8035df469898a 9@msnews.micros oft.com...[color=blue]
                          > bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=green]
                          > > Mine builds ok on 2003. Bloody odd.
                          > >
                          > > public static const int WM_COMMAND = 0x112;
                          > >
                          > > Works for me.[/color]
                          >
                          > Doesn't work for me with VS.NET 2003. Is it definitely, definitely
                          > building that file? Quick way to check: if you change it to
                          >
                          > public xxx const int WM_COMMAND = 0x112;
                          >
                          > does it *then* give a compiler error?
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • bwahahahaha

                            #14
                            Re: C# const declaration doc error

                            I cant check it now it wasnt on this machine but ill recheck again tomorrow.


                            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                            news:MPG.19f8db ba8035df469898a 9@msnews.micros oft.com...[color=blue]
                            > bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=green]
                            > > Mine builds ok on 2003. Bloody odd.
                            > >
                            > > public static const int WM_COMMAND = 0x112;
                            > >
                            > > Works for me.[/color]
                            >
                            > Doesn't work for me with VS.NET 2003. Is it definitely, definitely
                            > building that file? Quick way to check: if you change it to
                            >
                            > public xxx const int WM_COMMAND = 0x112;
                            >
                            > does it *then* give a compiler error?
                            >
                            > --
                            > Jon Skeet - <skeet@pobox.co m>
                            > http://www.pobox.com/~skeet
                            > If replying to the group, please do not mail me too[/color]


                            Comment

                            • Brian W

                              #15
                              Re: C# const declaration doc error

                              FWIW, I get a compiler error, as expected, on (yes, I'm using VS.NET 2003)

                              public class MyClass
                              {
                              public static const int WM_COMMAND = 0x112;
                              }


                              The constant 'xxx.WM_COMMAND ' cannot be marked static


                              Brian W



                              "bwahahahah a" <bwahahaha@bwah ahaha.org.ie> wrote in message
                              news:OG48sIAlDH A.2616@TK2MSFTN GP11.phx.gbl...[color=blue]
                              > Mine builds ok on 2003. Bloody odd.
                              >
                              > public static const int WM_COMMAND = 0x112;
                              >
                              > Works for me.
                              >
                              >
                              > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              > news:MPG.19f8d5 7b28af91279898a 7@msnews.micros oft.com...[color=green]
                              > > bwahahahaha <bwahahaha@bwah ahaha.org.ie> wrote:[color=darkred]
                              > > > The documentation states that its not possible to declare them static,[/color][/color]
                              > when[color=green][color=darkred]
                              > > > 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.[/color]
                              > >
                              > > 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 - <skeet@pobox.co m>
                              > > http://www.pobox.com/~skeet
                              > > If replying to the group, please do not mail me too[/color]
                              >
                              >[/color]


                              Comment

                              Working...