"Step through" enums

J

Jon Slaughter

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
 
M

Marc Gravell

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

Marc
 
J

Jon Slaughter

Marc Gravell said:
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
 
M

Marc Gravell

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
 
C

Carl Daniel [VC++ MVP]

Jon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top