By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

"Step through" enums

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
foreach (YourEnumType value in Enum.GetValues(typeof(YourEnumType))) {
// do something with value
}

Marc
Nov 6 '06 #2

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.