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":

                        ...
dhalberg
  • 155
  • 1
  • 1
  • 7
  • What is the reason for the loop and the switch here? What is it that you are trying to do really? – Guffa Nov 05 '09 at 02:07

4 Answers4

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

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

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • @Guffa: that's better, thanks. @dhalberg: Not sure I understand your comment. My edit may make things more clear. – Michael Petrotta Nov 05 '09 at 01:42
  • @dhalberg: If you're looking for a friendly description for the enum value, rather than just its name, see http://stackoverflow.com/questions/1415140/c-enums-can-my-enums-have-friendly-names/1415187#1415187, as shf301 suggests in his answer. – Michael Petrotta Nov 05 '09 at 01:47
  • But I still think that it's better to loop the enum values instead of their string representation. – Guffa Nov 05 '09 at 01:48
  • 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). – dhalberg Nov 05 '09 at 01:51
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.

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
  • 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. – Guffa Nov 05 '09 at 01:43
  • 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. – dhalberg Nov 05 '09 at 01:44
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();
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Why the downvote? It's pretty pointless if you don't say what it is that you don't like... – Guffa Nov 05 '09 at 01:46
  • 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 Nov 05 '09 at 02:05
  • 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. – Guffa Nov 05 '09 at 06:43
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

Community
  • 1
  • 1
shf301
  • 31,086
  • 2
  • 52
  • 86