0

My enum class PlayerPosition holds all of the positions on a football field (QB, HB, etc.). I'm having trouble "translating" (not sure of the word but I hope you understand) the enum from the # it's stored as into a string, which can be read by my switchboard. Right now, I already have a "foreach" loop in this method (called Calc) but I can't have a "foreach" loop in this method because this method is already in a loop to deal with getting to the next player. Anywho, this is the code I came up with. I've tried Enum.GetNames(typeof(PlayerPosition)) in the switch parentheses but it didn't work.

 foreach (string p in Enum.GetNames(typeof(PlayerPosition))) { switch (p) { case "QB": ... 
1
  • What is the reason for the loop and the switch here? What is it that you are trying to do really? Commented Nov 5, 2009 at 2:07

4 Answers 4

4
PlayerPosition position = PlayerPosition.Quarterback; string name = position.ToString(); 

EDITed with Guffa's recommended change from Enum.GetName to ToString().

Sign up to request clarification or add additional context in comments.

4 Comments

@Guffa: that's better, thanks. @dhalberg: Not sure I understand your comment. My edit may make things more clear.
@dhalberg: If you're looking for a friendly description for the enum value, rather than just its name, see stackoverflow.com/questions/1415140/…, as shf301 suggests in his answer.
But I still think that it's better to loop the enum values instead of their string representation.
Yeah, but I wouldn't know how to call the method from the Main() method. I have this particular method (Calc) in a loop as such: foreach (team t in league.Teams), foreach (Player p in t.Players), p.Calc. That's the only way I've found to get to this method (CALC).
3

Don't Do That

There's no need to convert the enum to a string for a switch statement, use the enum values in the switch statement and foreach loop instead.

Do This Instead

//this code was not tested, but hopefully you get the idea... foreach(PlayerPosition pp in PlayerPosition.GetValues()) { case PlayerPosition.Quarterback: ... break; ... } 

An Even Better Solution

Would be to eliminate the switch statement entirely by taking advatage of polymorphism. How to do this depends on what exactly you're trying to do in the body of the switch statement.

2 Comments

GetValues is a static method in the Enum class, you can't use it on an enum. Check the code in the answer that I posted before.
I thought I said I can't have such a loop in the method because the method itself is in a loop to handle all of the players. Maybe I'm misunderstanding. I'll try this.
1

Loop the enum values instead, and turn them into strings only when you need the actual string:

foreach (PlayerPosition p in Enum.GetValues(typeof(PlayerPosition))) { switch (p) { case PlayerPosition.QB: ... } string positionText = p.ToString(); } 

2 Comments

It wasn't me. I've implemented your idea, but how do I get to the Calc method from the Main() method without looping? The Calc method is in the Player class--a different class from where the Main() method resides.
dhalberg: All you need to call the method is a reference to an instance of the class containing the method. That has nothing to do with looping. If you want to call the method for each item in a collection then the looping is natural to acccess each item, but the loop is not directly related to calling the method.
1

Check out the highest ranked answer to this question on adding the Description attribute to your enums which will let you associate a friendly name with your enum values

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.