# loop though an enum in order

• ### Question

• So I have an enum  called slots and I was to loop though all slot in order   from slot1 - slot25.

here is my code :

```int i = 1;
{
BSM.SlotMove(slots );
MessageBox.Show("Slot" + Convert.ToString(i+1) + " Move Done");
i++;
}```

the problem is that the slots are int but values that are not indoor.

and the above code seem to do in oder from smallest number to biggest.

but i need it to go in order of slot1- slot25.

how do I do that ?

• Edited by Thursday, March 7, 2019 2:18 PM
Thursday, March 7, 2019 1:42 AM

### All replies

• So I have an enum  called slots and I was to loop though all slot in order   from slot1 - slot25.

here is my code :

int i = 1;
{
BSM.SlotMove(slots );
MessageBox.Show("Slot" + Convert.ToString(i+1) + " Move Done");
i++;
}

the problem is that the slots are int but values that are not indoor.

and the above code seem to do in oder from smallest number to biggest.

but i need it to go in order of slot1- slot25.

how do I do that ?

Of course, the solution is very simple!

But I have a question for you: why don't you use the tool to insert code properly?

Thursday, March 7, 2019 5:06 AM
• Hi Btb4198,

Thank you for posting here.

For your question, you want to show the value of slots in order of slot1- slot25.

You could try the following code.

```class Program

{

{

public   enum Slots {test1,test2,test3,test4,test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25 };

}

static void Main(string[] args)

{

int i = 1;

{

Console.WriteLine("Slot" + Convert.ToString(i) +"   " +slots.ToString()+"   Move Done");

i++;

}

}

}```

Result:

Best regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Thursday, March 7, 2019 5:41 AM
• Hi Btb4198,

Thank you for posting here.

For your question, you want to show the value of slots in order of slot1- slot25.

You could try the following code.

```class Program

{

{

public   enum Slots {test1,test2,test3,test4,test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20, test21, test22, test23, test24, test25 };

}

static void Main(string[] args)

{

int i = 1;

{

Console.WriteLine("Slot" + Convert.ToString(i) +"   " +slots.ToString()+"   Move Done");

i++;

}

}

}```

Result:

Best regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

1- Why did you provide an answer, before asking the OP to use the tool to insert code properly?

2- Why did you post code with very bad formatting?

3- Why did you post code that does not answer the OP?

Notice that your code does not output the named constants of the enum in sorted order (as required).

Your code fails if you replace test1 by test51!

Thursday, March 7, 2019 6:17 AM
• If the order of numeric values does not correspond to the desired order of names, then try this too:

```var names = Enum.GetNames( typeof( Slots ) ).OrderBy( n => int.Parse( n.Substring( "slot".Length ) ) );

foreach( var name in names )
{
Slots slot = (Slots)Enum.Parse( typeof( Slots ), name );
BSM.SlotMove( slot );
. . .
}```

or this:

```var ordered_slots = Enum.GetValues( typeof( Slots ) ).Cast<Slots>().OrderBy( s => int.Parse( s.ToString().Substring( "slot".Length ) ) );

foreach( Slots slot in ordered_slots )
{
BSM.SlotMove( slot );
. . .
}
```

• Edited by Thursday, March 7, 2019 6:59 AM
Thursday, March 7, 2019 6:54 AM
• If you use this enum:

enum Slots{Red,Yellow,Green,Blue}

Cant you ask the OP to use the tool to insert code properly?

• Edited by Thursday, March 7, 2019 7:04 AM USE THE TOOL
Thursday, March 7, 2019 7:03 AM

• Cant you ask the OP to use the tool to insert code properly?

Can you please shut up? You are just annoying! And please do not post your rubbish# code ritehere!

@OP just ignore him...

```        public enum Slots
{
Slot4,
Slot12,
Slot13,
Slot5,
Slot6,
Slot17,
Slot18,
Slot19,
Slot20,
Slot11,
Slot14,
Slot1,
Slot2,
Slot3,
Slot15,
Slot16,
Slot24,
Slot25,
Slot21,
Slot22,
Slot23,
Slot7,
Slot8,
Slot9,
Slot10
}
public static void Main(string[] args)
{
var values = Enum.GetValues(typeof(Slots))
.Cast<Slots>()
.OrderBy(x => int.Parse(Regex.Match(x.ToString(), "\\d{1,2}").Value))
.ToList();
foreach(var slot in values)
{
Console.WriteLine(\$"{slot} Move Done (int value: {(int)slot})");
}
}```

```            Enum.GetValues(typeof(Slots))
.Cast<Slots>()
.OrderBy(x => int.Parse(Regex.Match(x.ToString(), "\\d{1,2}").Value))
.ToList()
.ForEach(slot => Console.WriteLine(\$"{slot} Move Done (int value: {(int)slot})"));```

• Edited by Thursday, March 7, 2019 10:50 PM
Thursday, March 7, 2019 10:12 PM
• Hi

Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

Best Regards,

Jack

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Tuesday, March 12, 2019 6:22 AM
• Can you please shut up? You are just annoying! And please do not post your rubbish# code ritehere

What the hell is this?

SHUT UP YOU, dude ssa eloh, aka rubbish# coder.

Nothing I wrote is wrong(!) and I didn't post any code.

Why do you accuse me, when I did NOTHING wrong?

Are you ANGRY because I refer to you as RUBBISH# CODER?

But you are, and every time I proved you are!

See this code you posted: it i rubbish# code.

If you wanna see highly elevated and advanced code, see my next post, in reply to the OP.

Shut up you, rubbish# coder.

• Edited by Tuesday, March 12, 2019 11:06 AM missing img
Tuesday, March 12, 2019 10:57 AM
• So I have an enum  called slots and I was to loop though all slot in order   from slot1 - slot25.

here is my code :

```int i = 1;
{
BSM.SlotMove(slots );
MessageBox.Show("Slot" + Convert.ToString(i+1) + " Move Done");
i++;
}```

the problem is that the slots are int but values that are not indoor.

and the above code seem to do in oder from smallest number to biggest.

but i need it to go in order of slot1- slot25.

how do I do that ?

Thank you for using the code insertion tool.

Here's my suggestion:

```public static void Main(string[] args)
{
var slots = (Slots[])Enum.GetValues(typeof(Slots));