473,382 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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

Feb 9 '07 #1
3 1538
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 Kesting
Feb 9 '07 #2
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" wrote:
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
Feb 9 '07 #3
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" <Jo***********@discussions.microsoft.comwrote in message
news:13**********************************@microsof t.com...
>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

Feb 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
0
by: TRUMPh | last post by:
Context: I have a list with indeterminately entrys @design time. The user should choose one of the item via property grid. I create a classes and a instances of enums @RunTime. For each...
1
by: Michael C | last post by:
Simple question. Is there any reason an enum cannot inherit from a string or guid? Cheers, Michael
7
by: Sathyaish | last post by:
I forgot the syntax for strongly typed enums in .NET. I know C# supports them. Not sure if strong-typing of enums is CLS compliant but, can you refresh my memory with the C# syntax and VB syntax,...
7
by: Wolf | last post by:
Hi all Who can Help me with this one? if I have this Enums: #region Title public enum Title: short { NA = 0,
13
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to...
5
by: Thierry | last post by:
Hello fellow pythonists, I'm a relatively new python developer, and I try to adjust my understanding about "how things works" to python, but I have hit a block, that I cannot understand. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.