This seems like a long shot, but I wondered if there's a way to loop
over the values in an Enum. For example, say I have an Enum as
follows:
Public Enum statusValues
Ready
Running
Finished
Error
End Enum
'.....
I'd like to be able to loop over and print out these values' names
something like this:
For Each item in statusValues
Console.WriteLine(item.ToString)
Next
....the reason being that, if I add elements to my Enum object, I won't
have to go change the code that prints out their names. Thanks!
--Frank 3 15021
On 25 Nov 2003 07:30:10 -0800, giant food wrote: This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows:
Public Enum statusValues Ready Running Finished Error End Enum
'.....
I'd like to be able to loop over and print out these values' names something like this:
For Each item in statusValues Console.WriteLine(item.ToString) Next
...the reason being that, if I add elements to my Enum object, I won't have to go change the code that prints out their names. Thanks!
--Frank
The Enum class has static methods that allow you to do this.
For example, to get the name of a specific enum value that was passed to
you, you could use
string name = Enum.GetName(typeof(MyEnumType),SomeEnumValue)
--
Roy Osherove
weblog: http://www.iserializable.com
Frank,
Remember, all enums inherit from System.Enum, System.Enum has shared methods
for retrieving information about Enums.
Try something like: For Each item in [Enum].GetValues(GetType(statusValues)) Console.WriteLine(item.ToString) Next
If you want the names of the Enum instead of the values, try:
For Each item in [Enum].GetNames(GetType(statusValues)) Console.WriteLine(item.ToString) Next
The [Enum] is an escaped identifier it allows me to refer to the System.Enum
type instead of the Enum keyword used to define an Enum.
Other useful members of System.Enum include: Format, GetName, IsDefined,
Parse, and ToObject.
Hope this helps
Jay
"giant food" <di**********@yahoo.com> wrote in message
news:a6**************************@posting.google.c om... This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows:
Public Enum statusValues Ready Running Finished Error End Enum
'.....
I'd like to be able to loop over and print out these values' names something like this:
For Each item in statusValues Console.WriteLine(item.ToString) Next
...the reason being that, if I add elements to my Enum object, I won't have to go change the code that prints out their names. Thanks!
--Frank
* di**********@yahoo.com (giant food) scripsit: This seems like a long shot, but I wondered if there's a way to loop over the values in an Enum. For example, say I have an Enum as follows:
Public Enum statusValues Ready Running Finished Error End Enum
'.....
I'd like to be able to loop over and print out these values' names something like this:
For Each item in statusValues Console.WriteLine(item.ToString) Next
\\\
Dim s As String
For Each s In [Enum].GetNames(GetType(KnownColor))
Me.ListBox1.Items.Add(s)
Next s
///
Replace 'KnownColor' with the name of your enum.
--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Glenn Venzke |
last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an
enum. Is it possible to pass the item name without the enum name in your
calling statement?
EXAMPLE:
public enum...
|
by: Vaclav Haisman |
last post by:
Motivation:
I have been working on some project recently that uses lots of enums with
disjunctive intervals of values because it is rather convenient way to
define series of constants with...
|
by: Richard |
last post by:
Okay gang,
This should be simple but apparently it's not... I want
to use the System.DayOfWeek enum to create and access an
array of objects with one object for each day of the
week. I'd like...
|
by: Adam Blair |
last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time
error is raised if not all values within the Enum are handled in the switch
statement? I realise you can use default:...
|
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
I have created an enum list like this:
enum myEnum : int
{
This = 2,
That,
NewVal = 10,
LastItm
|
by: Fred Zwarts |
last post by:
What is the recommended way to loop over all enum values of a certain enum type?
Consider the following definition:
typdef enum {A=2, B, C=5, D} E;
then
for (E x = A; x <= D; ++x) { ... }
|
by: Steven Nagy |
last post by:
So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?
There's now a nice little code...
|
by: bsma1 |
last post by:
I building a web service that has an enum I want the consuming
application to be able to use.
I have the enum declared in the web service as:
public enum myEnum
{
ONE = 1,
TWO = 2,
};
|
by: jerry |
last post by:
i have written a simple phonebook program,i'll show you some of the
codes,the program's head file is member.h . i suppose the head file
works well.so i don't post it. here's the clips of main...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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...
|
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: 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...
| |