public static const member?
-
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. -
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.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
-
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.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;