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

"Step through" enums

is there a simple way to "step" through enums? I have a button that I want
to click and have it "cycle" through a set of states defined by enums but
the only way I can think of doing this "properly" yet "ugly" is to test the
state for each state. I know that enumes are not ordered but since they are
"stored" as numbers they have an inherent ordering which can be used. I
don't really care about the ordering though but just the ability to step
through them.

Obviously I could define my first entry as, say, equivilent to 0 and the
last as n and then simply treat them as numbers but this doesn't seem
elegant.

Thanks,
Jon
Nov 6 '06 #1
4 2981
foreach (YourEnumType value in Enum.GetValues(typeof(YourEnumType))) {
// do something with value
}

Marc
Nov 6 '06 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
foreach (YourEnumType value in Enum.GetValues(typeof(YourEnumType))) {
// do something with value
}

Marc
I don't see how this allows you to cycle through the enums? What I mean by
this is not step through the enums but have an instance cycle through
them...

equivilently with integers

(i >= max_enums) ? i = 0; : i++;

I need a similar method that is easy when dealing directly with the enums.

I can do some_enum++; to move to the next one but eventually I will have it
go past the last enum and not cycle back. I suppose I can use the foreach
to test and see if some_enum is not equal to any of the enums and then
"reset" it but this doesn't seem like a good way.

Jon
Nov 6 '06 #3
I see what you mean now... well, you could check to see if there is anything
returned (Enum.GetValues) after the current one, then loop back to the
start, but yes it is a little hacky.

But I would personally avoid (like the plague) treating enums as integers...
all you need is somebody to decide (for whatever reason) that the underlying
numbers are 7, 2,1324,1,99999 in that order and the integer method falls
over.

Marc
Nov 6 '06 #4
Jon Slaughter wrote:
"Marc Gravell" <ma**********@gmail.comwrote in message
news:eJ**************@TK2MSFTNGP02.phx.gbl...
>foreach (YourEnumType value in Enum.GetValues(typeof(YourEnumType)))
{ // do something with value
}

Marc

I don't see how this allows you to cycle through the enums? What I
mean by this is not step through the enums but have an instance cycle
through them...

equivilently with integers

(i >= max_enums) ? i = 0; : i++;

I need a similar method that is easy when dealing directly with the
enums.
I can do some_enum++; to move to the next one but eventually I will
have it go past the last enum and not cycle back. I suppose I can
use the foreach to test and see if some_enum is not equal to any of
the enums and then "reset" it but this doesn't seem like a good way.
Add a level of indirection -

Put all of the enum values (from Enum.GetValues) into an array (or List<>)
and use an integer index into that structure. You can easily test the
integer to determine when you've reached the end of the list and need to
recycle.

-cd
Nov 6 '06 #5

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

Similar topics

2
by: Alex | last post by:
Greetings all, I'm trying to use the "opendir" command on Win32 in a CGI script. I'm using Apache 2.0.48 for Win32. The "openDir" command works from the CGI script when I try to open a...
1
by: scott | last post by:
Hi, Does anyone know how to make available the "step into stored procedure" option when connecting to a remote SQL server (on lan) using VS 2003 ? note - win xp pro - 2000 domain - 2003...
4
by: Benjamin Joldersma | last post by:
Hello all, My company is installing a Bluecoat caching machine to help serve some of our dynamic content. This is fine for free documents, but we have to make a minor architectural tweak for...
1
by: Big Dave | last post by:
I'm having trouble using the update command of a dataadapter. It would help if I could watch it go through each record in the datatable and see what it's doing. Is there a way to do this? Also,...
1
by: Tom | last post by:
Hi, I am currently on a project where one site needs to send the user credentials to another site, through web services. Scenario: * "User 1" will authenticate to "Site A" using NTLM ("Site...
6
by: Martijn Coppoolse | last post by:
Hello everyone, I've got two little apps which are able to create a window that displays something on the desktop, but are not clickable. Instead, when you click on the window, the item...
1
by: Klaas | last post by:
I have a dropdownlist. When the user clicks on a button I want the selected listitems text to be line-through. Is this possible in IE? or with some javascript? tia
2
by: Chris Song | last post by:
Hello, I'v installed mysql 5.0 (MySQL-server-standard-5.0.27-0.rhel4.i386.rpm) on RedHat AS4. When I start mysql through command line (i.e. /usr/bin/mysqld_safe --datadir=/var/lib/mysql...
3
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Does anyone know why I have no "Step Into" option during debugging in VS 2005/C# app? I have "Step Over" but no "Step Into" even though it would be stepping into one of my own functions inside the...
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:
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
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
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: 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:
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...

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.