473,387 Members | 1,574 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,387 software developers and data experts.

get name of enum element programmatically?

I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?

Zytan

Feb 27 '07 #1
3 18595
Zytan <zy**********@yahoo.comwrote:
I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?
Call ToString() on it:

using System;

enum Poster
{
Zykan,
Jon
}

class Test
{
static void Main()
{
Poster p = Poster.Jon;
string name = p.ToString();

Console.WriteLine (name);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #2
Zytan wrote:
I have an enum with a bunch of names / values. This is nice as I can
code using the names, instead of memorizing the values.

But, let's say something gives me a value. Now, while my code is happy
using a number, what if I want to display the name that this value
means?

Can I get the enum name, as it is typed in code, from the enum? or do
I need to write another array of strings to accomplish this?

Zytan
Just use the ToString method.

--
Göran Andersson
_____
http://www.guffa.com
Feb 27 '07 #3
Jon and Goran, you guys answer everything. Thanks for your help, I
appreciate it. ToString, of course, I should have guessed.

Zytan

Feb 28 '07 #4

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

Similar topics

7
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
2
by: Mark | last post by:
Assume you've declared an enum .... public enum MyEnum { First, Second, Third } Without knowing the values of the enum, is it possible to iterate through all the possible values...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
8
by: Mark P | last post by:
I'm working on a project where I have something like enum Modes {mode_a, mode_b, ...}; Due to project spec changes, the number of Modes has increased several times. There are a few places...
13
by: Rohit | last post by:
Is there any way by which I can find number of elements in an enum list? Generally I use one last element at the end to find out total number of elements. e.g. typedef enum{ SUN, MON, TUE,...
3
by: puzzlecracker | last post by:
What's the fastests way to find a number of values in enum. I came up with this. but I think it's over kill. enum Type { VAL1, VAL2, VAL3 }
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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,...

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.