-2

Let's say I have an enum like such:

public enum Something
{
    This = 10,
    That = 5,
    It = 11
}

I would like to know if its possible to get the next enum based on the order they are and not their value or name.

Unhappily I have no control over the numbers, I can only change the names.

For example, if I have That the next is It and not This.

Pseudo code:

var current = Something.That;
Console.WriteLine(current);
// prints That
current = GetNextEnum(Something.That);
// prints It
Console.WriteLine(current);
current = GetNextEnum(Something.It);
// prints This
Console.WriteLine(current);
// And so the cycle continues...

Is there any way to achieve this?


UPDATE:

I can't execute more than one state per pulse, so I need to know which state I have ran to know which state to run next, for example:

private Something _state = Something.That;
private void Pulse()
{
   // this will run every pulse the application does
   foreach (var item in (Something)Enum.GetValues(typeof(Something)))
   {
       if (_state == item)
       {
           // Do some stuff here
       }
       _state = next item;
       return;
   }
}

Am also trying to avoid to make a block for each state and rather have the states being executed dynamically as they can be added or removed.

So my real question is how can I know what comes next and where I am.

Guapo
  • 3,446
  • 9
  • 36
  • 63
  • 1
    Use a `List`, then you have a real order. – Tim Schmelter Mar 19 '15 at 21:23
  • Here is a thread that might help [link](http://stackoverflow.com/questions/972307/can-you-loop-through-all-enum-values) – jcjunction Mar 19 '15 at 21:24
  • As far as I know, the answer is no. – Robert McKee Mar 19 '15 at 21:34
  • @TimSchmelter see upate – Guapo Mar 19 '15 at 21:34
  • Put them in numerical order in which they run, and just add one? – Robert McKee Mar 19 '15 at 21:35
  • @RobertMcKee they don't run in a numerical order as in their id unhappily and the ids are predefined and I cant change the ids. – Guapo Mar 19 '15 at 21:36
  • Don't use an enum. Make it a class that understands its own state, and use that. The class can then automatically wrap around at the end, or report and error or whatever you need. Otherwise, you will end up with an enum and helper functions, and at that point, you have a class in every aspect but declared. – StarPilot Mar 19 '15 at 22:23
  • `{Type}.GetFields(BindingFlags.Public | BindingFlags.Static)` *seems* to return the values in the right order, but MSDN [says](https://msdn.microsoft.com/en-us/library/ch9714z3) "*The GetFields method does not return fields in a particular order*"... **OR** you could use custom attributes, like in [this](http://stackoverflow.com/a/25147851/1364007) SO answer to "*Sort enums in declaration order*". – Wai Ha Lee Mar 19 '15 at 22:56

1 Answers1

0
public Something GetNextEnum(Something e)
{
  switch(e)
  {
     case This:
       return That;
     case That:
       return It;
     case It:
       return This;
     default:
       throw new IndexOutOfRangeException();
  }
}

Or make it an extension:

public static class MySomethingExtensions {
    public static Something GetNextEnum(this Something e)
    {
      switch(e)
      {
         case This:
           return That;
         case That:
           return It;
         case It:
           return This;
         default:
           throw new IndexOutOfRangeException();
      }
    }
}

and you can use it like this:

_status=_status.GetNextEnum();
Robert McKee
  • 21,305
  • 1
  • 43
  • 57