I just don't understand how enums work.....Help needed

G

Guest

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
 
H

Hans Kesting

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

With that "public enum Maker" you don't define a property "Maker" with those
possible values,
you just define an inner type.

You want something like this:
namespace ns
{
public enum Maker {Nintendo, Sega, Sony, Panasonic}

class GamesConsole
{
public int iReference;
public Maker theMaker;
}
}

static void Main(string[] args)
{
GamesConsole myConsole = new GamesConsole;
myConsole.theMaker = ns.Maker.Sega;
}


Of course it would be better to use private variables wrapped in a public
property,
but for demo-purposes this is enough.


Hans Kestin
 
G

Guest

Smith,

You are mistaken a little about enums. You cann't assign a value to enum at
the instance, but only can extract the value of the enum.

To put in simple words the code you mentioned has to be used as mentioned
below
GamesConsole myConsole = new GamesConsole;

myConsole.Maker = 2; // Wrong
int intSelectedGame = myConsole.Maker.Sega;

And the variable intSelectedGame will have the value of the Sega position.

Hope this helps you
--
Every thing is perfect, as long as you share!!!


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
 
P

PokerMan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top