0

Suppose I have an enum with arbitrary increments between contents. Like so:

 typedef enum {this=2,that=5,whoa=6}example;

Now I want to enumerate through them. If the contents were incremented by 1 for each item, then this would be easy with a simple for loop. I could still use a for but test for each option in example enum before proceeding with an operation, but that seems rather inefficient. Is there a standard way of enumerating through an enum?

johnbakers
  • 22,776
  • 20
  • 106
  • 230
  • I found the answer [here][1]. [1]: http://stackoverflow.com/questions/105372/how-to-enumerate-an-enum – johnbakers Apr 03 '12 at 15:22
  • That is actually not the answer, at least, if your a looking for a solution in Objective-C (as you tagged). The answer applies for C#. – Matthias Apr 03 '12 at 15:31

1 Answers1

2

enums are basically integer constants. To simulate sets (I guess, that is what you want) use dictionaries. Then, you can easily iterate over all elements.

Matthias
  • 7,507
  • 2
  • 23
  • 49
  • i don't think this is entirely true. i have seen examples where methods are used to enumerate through an enum, but that was for sequential ordered enums. – johnbakers Apr 03 '12 at 01:49
  • Of course you *can* do it, but this is nothing but using the fact, that in this case the *set* of numbers is also a *sequence* of numbers. However, this is not true in the general case. – Matthias Apr 03 '12 at 03:53
  • answer was found here: http://stackoverflow.com/questions/105372/how-to-enumerate-an-enum – johnbakers Apr 03 '12 at 15:23
  • An enum in C# is something quite different than an enum im C. In C#, you have a "real" set type, that you can *convert* to an integer, see e.g., http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx. In C (and Objective-C derives here from C), an enum is only a special declaration for integer constants. – Matthias Apr 03 '12 at 15:29