   [.net] Defining a constant in c#
  I am using VS 2k5 Standard with C#.  I realize that I can no longer #define a constant as I did in C/C++ and googling around I found 2 ways around this.  One says to use enums like soenum USER_CODES{    USER_NEW,    USER_GOOD,    USER_SPOOF,    USER_REMOVED,    USER_NOT_FOUND}    //and later on to use them  USER_CODES code = USER_CODES.USER_GOOD;  if (code == USER_CODES.USER_GOOD) Console.WriteLine("Code is acceptable");  if (code == USER_CODES.USER_SPOOF) Console.WriteLine("Code is spoofed by user");or the other source says to just use a class like so...class MyConstants    {   public const int A = 1;    }// And farther downif (code == MyConstants.A) Console.WriteLine("Code is A");My question is, which is the better/more acceptable/standard way to do it?  Or does it just boil down to programming preference?  Will I run into any problems using either way down the line?  Thanks in advance!PS Anyone else getting lots of "bandwidth exceeded" pages here in Gamedev?   
  It really depends on a few factors.Enumerations are great for any kind of sequence or list.  They are also great for any kind of bitfield which is a special type of sequence.  Enumerated types can be listed by name, which is often a benefit.If any of those are true, use an Enum.Your second style of a publicly exposed variable is usually frowned upon.  It is usually better to use an accessor for that sort of thing:const int a = 5;public int A { get { return a; } }  \\ You could just use return 5That makes it easier for others who derive from your class to override that value later if they need to.If you only have one or two of them, you don't expect the enumeration to ever grow, and you might need to change it in a derived class, use an accessor.   ;Quote:Original post by frobYour second style of a publicly exposed variable is usually frowned upon.No, it isn't. Show me anyone who "frowns" upon a public const and I'll punch them in the mouth.Quote:  It is usually better to use an accessor for that sort of thing:const int a = 5;public int A { get { return a; } }  \\ You could just use return 5That makes it easier for others who derive from your class to override that value later if they need to.If they can do that, it's no longer a const.;  Here's how I see the difference between an enum and a const.enum is a new type, which can take a fixed number of discrete values. Often (but not always), the actual values assigned by the compiler to the enum elements doesn't really matter. I personnally think that your USER_CODES example is a good one. Here's another one: enum Alignment { Left, Center, Right, Justified };const is just a magic value. If you have many related consts, you should consider whether they aren't an enum. Some examples of consts:const double Pi = 3.14;const int MaxNumberOfConnections = 15;const char ArgumentSeparator = '&';Of course, this may be a question of style, and you can do it another way.BTW: I agree with Arild. IMHO, you should not wrap a constant into a property. At least not if it's a true constant.Regards,jods   ;  Ok, so I can use the enum method if I don't care the number assigned (USER_CODES example) and just use "const" when I need to specify a certain value.  Thanks for all the help folks!BTW:  The error pages I have been receiving are from my firefox browsers settings being toyed with by an extension.  Stay away from the accelerator extension.;  As an aside, you can also use the readonly keyword to set the value of a constant to which you don't know the value upon declaring the class.  In this example, we give a player a name to which they can't use.  Since we don't know the name of the player, but want it to stay consistant once its set, we make it readonly.public class Player{    readonly string name;   public Player(string playerName)    {   name = playerName; // works for initialization /* .... */   name = "Try to change name"; // denied changing    }}