Yes important thing to note.
Enums are there to make code more readable, not as some clever way of
assigning values.
So like Chak said:
myConsole.Maker = 2; // Wrong
int intSelectedGame = myConsole.Maker.Sega;
As you can see Chaks line is far more readable. You can immediately see the
selected game is Sega make. Witht he 2 you'd have to search through code and
see what 2 identifies too
Especially useful, enums, for when you read identifiers out of a database.
You can then map them to much more readable expressions. Another good use
for them is when you add the Description attribute and use that. because
then you can do this for front end use:
Maker myMaker = myConsoleMaker.Maker.Sega; //set some variable to sega enum
myTextBox.Text = myMaker.ToString();
And the text box will say "Sega". Or if you use a description attribute
could say whatever you specified as the description, like "Sega Console" etc
etc this does require telling it to use the attribute instead of ToString().
A lot can be done with enums but i wont go into it all, thats a fair run
down i hope.
Happy coding.
Jonathan Smith said:
I have a class as follows:
class GamesConsole
{
public int iReference;
public enum Maker {Nintendo, Sega, Sony, Panasonic}
}
I then use this from another class:
class Test
{
static void Main(string[] args)
{
GamesConsole myConsole = new GamesConsole;
myConsole.Maker = 2;
}
}
Can anyone tell me how the hell i use enums??????????
THanks