473,407 Members | 2,314 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,407 software developers and data experts.

[C#: Windows] enum uhh enuming?

Plater
7,872 Expert 4TB
I saw it referenced in a post here and now I'm kicking myself for not marking it.
Is there a way to enumerate through your enums?

Supose you have:
Expand|Select|Wrap|Line Numbers
  1. public enum PeriphType
  2.     {
  3.         PTYPE_ERROR,
  4.         PTYPE_NONE,
  5.         PTYPE_HOST,
  6.         PTYPE_TEMPHUM,
  7.         PTYPE_DUST,
  8.         PTYPE_AIRPRESS,
  9.         PTYPE_DCVOLTS
  10.     };
  11.  
I know I can do this:
Expand|Select|Wrap|Line Numbers
  1. string mys=PeriphType.PTYPE_NONE.ToString();
  2. //mys is now ="PTYPE_NONE"
  3.  
I'm looking for something along the lines of:
Expand|Select|Wrap|Line Numbers
  1. foreach(<something> i in <auto-collection of all the PerhipType enums>)
  2. {
  3.    string mys=i.ToString();
  4. }
  5.  
I'm really mad I can't remember it. Something to do with current assemblies? I dunno.

Any ideas?
Nov 7 '07 #1
3 1654
Shashi Sadasivan
1,435 Expert 1GB
Hi Plater,
you got me with this one !!
I remember doing something like this a few weeks ago, unfortunately had to scrap it.

this iterates the values
Expand|Select|Wrap|Line Numbers
  1. foreach (PerhipType ptype in System.Enum.GetValues(typeof(PerhipType )))
  2. {
  3. //code is here
  4. }
this iterates the names
Expand|Select|Wrap|Line Numbers
  1. foreach (string str in System.Enum.GetNames(typeof(PerhipType )))
  2. {
  3. //code is here
  4. }
I think it is like that !!!
Nov 7 '07 #2
balabaster
797 Expert 512MB
Hi Plater,
you got me with this one !!
I remember doing something like this a few weeks ago, unfortunately had to scrap it.

this iterates the values
Expand|Select|Wrap|Line Numbers
  1. foreach (PerhipType ptype in System.Enum.GetValues(typeof(PerhipType )))
  2. {
  3. //code is here
  4. }
this iterates the names
Expand|Select|Wrap|Line Numbers
  1. foreach (string str in System.Enum.GetNames(typeof(PerhipType )))
  2. {
  3. //code is here
  4. }
I think it is like that !!!
This is how I have it in one of my projects...with names changed to protect those otherwise incriminated ;)

VB for you because I know how you hate that :oP
Expand|Select|Wrap|Line Numbers
  1. Dim MyEnumItems() As MyEnum = System.Enum.GetValues(GetType(MyEnum))
  2. For Each EnumItem As MyEnum In MyEnumItems 
  3.   Console.WriteLine(EnumItem & ": " & CType(EnumItem, Integer))
  4. Next
C#
Expand|Select|Wrap|Line Numbers
  1. MyEnum[] MyEnumItems = (MyEnum[])System.Enum.GetValues(typeof(MyEnum));
  2. foreach (MyEnum EnumItem in MyEnumItems){
  3.   Console.WriteLine(EnumItem+": "+(int)EnumItem);
  4. }
...and GetNames just gets the string names and GetValues actually gets the enumeration item as opposed to just the value which means you have access to both the value and the string.

If you use GetValues and you only require the value, cast it as an integer, otherwise by reference to the EnumItem you get the string name.
Nov 7 '07 #3
Plater
7,872 Expert 4TB
Thanks, I can't even remember why I wanted it, it's been so long (I suspect it was from when I was coding an smtp client).
I was really kicking myself for not knowing how simple it was.

Edit: for the immediate time, it doesn't help me since it's not available in the compact framework
Nov 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gey-Hong Gweon | last post by:
Is there a way to copy a PIL image to windows clipboard (as a dib image or a bitmap, I suppose)? What I would like to accomplish is to do a fast copy and paste of images from my python...
3
by: James | last post by:
I can get the windows version number from System.Environment.OSVersion e.g. "Windows NT 5.1.2600.0" for XP SP2. I was wondering whether this tells you the type of windows it is or if you have to...
10
by: ThunderMusic | last post by:
Hi, How can I get if the Windows running is a server version? Is there a property somewhere that can tell me if the Windows version is a server edition (including server, advanced server, web...
18
by: jrhoads23 | last post by:
Hello, I am trying to find a way to tell if an .NET windows forms Button (System.Windows.Forms.Button) is "depressed" (pushed down). For my application, I can not use a check box control set to...
3
by: Christian Blackburn | last post by:
Hi Gang, I would like to have my program use the built in Move and Copy Dialogs that are used by Windows Explorer/My Computer. Thanks in Advance, Christian Blackburn
12
by: Jarod_24 | last post by:
I got a project called "Forms" that hold some forms and stuff in my solution. A argument at startup defines wether to use a From or Console. My plan was to create a myConsole class that would...
4
by: =?Utf-8?B?QXZhRGV2?= | last post by:
ASP.Net 2. We are migrating to Windows 2008 64 bit Server with IIS 7 from Windows 2003 32 Bit with IIS 6. A few library classes we wrote uses impersonation in code like explained in this...
1
by: moondaddy | last post by:
I need to use an ObjectDataProvider in a resource file to pull in the System.Window.TextAlignment enum which is part of the PresentationCore.DLL. I was able to do this with the...
23
by: Stewart Berman | last post by:
I am trying to develop a wrapper class for the Windows API functions in Visual Studio 2008: GetOpenFileName GetSaveFileName I put together a starter class: using System; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.