Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. public static const member?

public static const member?

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    I've declared several public static members in a card function. class Card { public static int VALUE_HEART = 0; public static char CHAR_HEART = 'h'; pubic static int VALUE_DEUCE = 0; public static char CHAR_DEUCE = '2'; // ... more for each suit and rank. } It is part of a class library used in several applications that pass data back and forth to make sure that the values are always the same. I just realized however, the my switch statements using these values throw errors: "A constant value is expected." But, declaring them as public static const int and public static const char throws errors as well: "The constant 'Cards.Card.VALUE_HEART' cannot be marked static." So, how do I fix this problem? The values need to be constant, but I need to be able to access them as static members: public static Suit ConvertToSuit(int i) { switch (i) { case VALUE_HEART: return Suit.Hearts; case VALUE_DIAMOND: return Suit.Diamonds; case VALUE_SPADE: return Suit.Spades; case VALUE_CLUB: return Suit.Clubs; } return Suit.Null; } ... Suggestions, please? As always, Thanks.

    S R 2 Replies Last reply
    0
    • B budidharma

      I've declared several public static members in a card function. class Card { public static int VALUE_HEART = 0; public static char CHAR_HEART = 'h'; pubic static int VALUE_DEUCE = 0; public static char CHAR_DEUCE = '2'; // ... more for each suit and rank. } It is part of a class library used in several applications that pass data back and forth to make sure that the values are always the same. I just realized however, the my switch statements using these values throw errors: "A constant value is expected." But, declaring them as public static const int and public static const char throws errors as well: "The constant 'Cards.Card.VALUE_HEART' cannot be marked static." So, how do I fix this problem? The values need to be constant, but I need to be able to access them as static members: public static Suit ConvertToSuit(int i) { switch (i) { case VALUE_HEART: return Suit.Hearts; case VALUE_DIAMOND: return Suit.Diamonds; case VALUE_SPADE: return Suit.Spades; case VALUE_CLUB: return Suit.Clubs; } return Suit.Null; } ... Suggestions, please? As always, Thanks.

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      In C#, consts are implicitly static. Declaring a variable const and static will result in a compiler error. I think your code should work fine once you take out the static modifier. From MSDN,The static modifier is not allowed in a constant declaration.[^] Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • B budidharma

        I've declared several public static members in a card function. class Card { public static int VALUE_HEART = 0; public static char CHAR_HEART = 'h'; pubic static int VALUE_DEUCE = 0; public static char CHAR_DEUCE = '2'; // ... more for each suit and rank. } It is part of a class library used in several applications that pass data back and forth to make sure that the values are always the same. I just realized however, the my switch statements using these values throw errors: "A constant value is expected." But, declaring them as public static const int and public static const char throws errors as well: "The constant 'Cards.Card.VALUE_HEART' cannot be marked static." So, how do I fix this problem? The values need to be constant, but I need to be able to access them as static members: public static Suit ConvertToSuit(int i) { switch (i) { case VALUE_HEART: return Suit.Hearts; case VALUE_DIAMOND: return Suit.Diamonds; case VALUE_SPADE: return Suit.Spades; case VALUE_CLUB: return Suit.Clubs; } return Suit.Null; } ... Suggestions, please? As always, Thanks.

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        static doesn't mean its constant. It could be set from the outside to another value. You should declare your variables like the following:

        public const int VALUE_HEART = 0;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups