473,671 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1556
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.theMa ker = 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.ToStrin g();

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.mic rosoft.comwrote in message
news:13******** *************** ***********@mic rosoft.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
2073
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 seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
99
6080
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 small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
27
2726
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 <typename Enum> class smart_enum { public: typedef Enum enum_type;
0
1322
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 needed/created enum a property spec will be defined - which will feed a PropertyBag. Problem: My Problem is that the PropertyGrid wont display a DropDown Menu for a new created Instance but give me an exception.
1
246
by: Michael C | last post by:
Simple question. Is there any reason an enum cannot inherit from a string or guid? Cheers, Michael
7
3617
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, please? I've been looking in "Professional C#" to no avail yet. I don't remember where I saw the syntax on an earlier occassion. This is just a small thing I am trying to memorize.
7
4721
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
5000
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 declare that an enum exists. i.e. It is within the 'spirit' of an interface to state that it exists. The implementing class will provide the members. e.g. I have a code library that performs a function. It exposes two interfaces which allow for...
5
2592
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 needed to output unicode datas back from a web service, and could not get back unicode/multibyte text before applying an hack that I don't understand (thank you google) I have realized an wxPython simple application, that takes the input
0
8393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6229
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.