1

How can I get the ordinal of an Enum in a for-loop?

I tried something like this:

Enum test {A,B,C}
byte[] helper =new byte[3];

foreach (tests i in Enum.GetValues(typeof(test)))
{ helper[i] == test.i; }

But this doesn't work...

In Java, I know you can use:

Enum test {A,B,C}
byte[] helper =new byte[3];

for (test i : test.values())
{ helper[i] == i.ordinal(); }

Is there something similar in c#? Or does anyone have a better idea for solving this issue?

here the original Parts of the Code(in Java):

public enum Facelet
{U1, U2, U3, U4, U5, U6, U7, U8, U9, R1, R2, R3, R4, R5, R6, R7, R8, R9, F1, F2, F3, F4, F5, F6, F7, F8, F9, D1, D2, D3, D4, D5, D6, D7, D8, D9, L1, L2, L3, L4, L5, L6, L7, L8, L9, B1, B2, B3, B4, B5, B6, B7, B8, B9}

enum color { U, R, F, D, L, B }
enum Corner{URF, UFL, ULB, UBR, DFR, DLF, DBL, DRB }
Byte ori;
final static Facelet[][] cornerFacelet = { { U9, R1, F3 }, { U7, F1, L3 }, { U1, L1, B3 }, { U3, B1, R3 },
        { D3, F9, R7 }, { D1, L9, F7 }, { D7, B9, L7 }, { D9, R9, B7 } };
public Color[] f = new Color[54];

and this is the for-loop:

               for (Corner i : Corner.values()) 
            {
                for (ori = 0; ori < 3; ori++)
                    if (f[cornerFacelet[i.ordinal()][ ori].ordinal()] == color.U || f[cornerFacelet[i.ordinal()][ori].ordinal()] == color.D)
                        break;}
Sir Ronon
  • 11
  • 5

1 Answers1

0

You can simply cast the enum object to int to get the ordinal value.

So:

int ordinalValue = (int) i;

Edit: after reading your updated OP I think this should be a step in the right direction. The only problem is the type mismatch between color and Color as described in the comments section.

class Program
{
    public enum Facelet
    {
        /* values */
    }

    enum color
    {
        /* values */
    }

    enum Corner
    {
        /* values */
    }

    public static Color[] f = new Color[54];

    private static Facelet[][] cornerFacelet =
    {
        new Facelet[] {Facelet.U9, Facelet.R1, Facelet.F3},
        new Facelet[] {Facelet.U7, Facelet.F1, Facelet.L3},
        new Facelet[] {Facelet.U1, Facelet.L1, Facelet.B3},
        new Facelet[] {Facelet.U3, Facelet.B1, Facelet.R3},
        new Facelet[] {Facelet.D3, Facelet.F9, Facelet.R7},
        new Facelet[] {Facelet.D1, Facelet.L9, Facelet.F7},
        new Facelet[] {Facelet.D7, Facelet.B9, Facelet.L7},
        new Facelet[] {Facelet.D9, Facelet.R9, Facelet.B7},
    };


    static void Main(string[] args)
    {
        foreach (Corner i in Enum.GetValues(typeof(Corner)))
        {
            int cornerOrdinal = (int) i;
            for (byte ori = 0; ori < 3; ori++)
            {
                Facelet currentCornerFacelet = cornerFacelet[cornerOrdinal][ori];
                int faceletOrdinal = (int) currentCornerFacelet;
                if (f[faceletOrdinal] == color.U || f[faceletOrdinal] == color.D)
                {
                    break;
                }
            }
        }
    }
}
nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • Yes, in this simple Case of corse, in my original Programm in Need the Value in an if-ask... `if (f[cornerFacelet[i.ordinal(), ori].ordinal()] == color.U || f[cornerFacelet[i.ordinal(),ori].ordinal()] == color.D)` this is how i make it in Java, but i dont want to poste this, be´cause out of context nobody knows what i want to do .... but thanks for halp :) – Sir Ronon Nov 29 '16 at 17:25
  • Well if you post your full code we could still help you, because the problem isn't with the context, it's with the way you are trying to use C#. – nbokmans Nov 29 '16 at 17:31
  • As I said, post the snippet of Java code you are trying to convert to C# in your OP, and I'll take a look – nbokmans Nov 29 '16 at 17:34
  • @SirRonon the `CornerFacelet` enum (in your Java example) is still missing, unless that is meant to be just `Facelet` – nbokmans Nov 29 '16 at 17:55
  • sry. this was an Typo.... i mean: `for(Corner i : Corner.values())` – Sir Ronon Nov 29 '16 at 18:00
  • Alright. Is this line `f[cornerFacelet[i.ordinal(), ori].ordinal()]` valid Java code? My Java IDE shows an error when I try to compile that. If it is not: am I right in assuming that what you basically want is the Facelet at position in the enum? (eg `cornerFacelet[0, 0]` = `U9` and `cornerFacelet[1, 1]` = `F1`) – nbokmans Nov 29 '16 at 18:03
  • Last question: you have an `enum color` but hold an array of `Color` (eg `public Color[] f = new Color[54]`. These are two different types (`color` and `Color`), so how is this a valid equation? `f[cornerFacelet[i.ordinal()][ ori].ordinal()] == color.U` - does the `color` enum inherit from `Color` class? – nbokmans Nov 29 '16 at 18:17