I don't know what you mean by "lock", but I think you mean you want each member of
Weapon
to have a constant value.
Assuming that is indeed what you mean,
The long answer:
Read http://msdn.microsoft.com/en-us/library/vstudio/2dzy4k6e.aspx
This will tell you everything you're asking about.
The short answer: yes. By default,
enum Weapon { Axe, Dagger, Sword, Mace }
will create the enum where Axe has value 0, Dagger has value 1, Sword has value 2, and Mace has value 3.
Suppose you want to decide your own values:
1 2 3 4 5 6 7
|
enum Weapon { Axe = 5;
Dagger; // = 6 by default (counts sequentially upward from last-defined value)
Sword = 9;
Mace = 11;
}
// Let's instantiate one
Weapon firstWeapon = Sword; // firstWeapon can be used as an int with value 9
|
If you want to use the Weapon name as, say, its damage value, that's how you'd do it.
If you're using multiple
enum
s, read the section of the article about
enum class
. It will likely save you a headache.
Edit: JLBorges answers the question more clearly and concisely. Written by a better thinker than myself.