3

I need to declare a List of Enums and I can't figure out how to do it right.

This is my code so far:

public enum Languages
{
    Ger,Eng,Fra,Ita,Rus
}

public class Player
{
    public string ID { get; private set; }
    private List<Languages> Languages;

    public Player(string ID, List<Languages> LangList)
    {
        this.ID = ID;
        this.Languages = LangList;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Player PlayerA = new Player("Player A", **[Problem is here]**);
    }
}

As you can see the constructor of Player expects a list of languages that are supported by the player. I wanted to define the possible Languages as enums. Now i don't know how i declare such a list in the construction call.

I know i could just do it like this:

        List<FoolMeLib.Languages> pLang = new List<FoolMeLib.Languages>;
        pLang.Add(FoolMeLib.Languages.Ger);
        pLang.Add(FoolMeLib.Languages.Eng);
        NewGame.AddPlayer(new FoolMeLib.Player("Player A", pLang));

but i want to write the code as sleek as possible... Btw: if you see any other bad practice: tell me. I'm triing to improve.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Stefan Lippeck
  • 381
  • 2
  • 17
  • [http://stackoverflow.com/questions/1167361/how-do-i-convert-an-enum-to-a-list-in-c](http://stackoverflow.com/questions/1167361/how-do-i-convert-an-enum-to-a-list-in-c) – T I Jun 01 '13 at 22:51
  • Try `public Player(string ID, IEnumerable LangList)` and pass an array with `new[] { Language.Ger, Language.Eng }`. – John Alexiou Jun 02 '13 at 00:14

6 Answers6

12

Without code changes to Player, you could do:

NewGame.AddPlayer(new FoolMeLib.Player("Player A",
    new List<FoolMeLib.Language> {
        FoolMeLib.Languages.Ger, FoolMeLib.Languages.Eng
}));

If you ware open to changes, a params FoolMeLib.Languages[] languages parameter would allow:

NewGame.AddPlayer(new FoolMeLib.Player("Player A",
    FoolMeLib.Languages.Ger, FoolMeLib.Languages.Eng));

Or if you allow the enum to be a [Flags] - just one parameter is needed:

NewGame.AddPlayer(new FoolMeLib.Player("Player A",
    FoolMeLib.Languages.Ger | FoolMeLib.Languages.Eng));

(But they would need specific numbers in the 2n range)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Good options for collection initializer have been posted.

Another options could be to use flags:

[Flags]
public enum Languages { Ger = 1,Eng = 2,Fra = 4,Ita = 8, Rus = 16 }

// ctor
public Player(string id, Languages languages) {}

// call
new Player("Player A", Languages.Eng | Languages.Fra);

Depending on what you plan to do with the languages this might be a good or a bad design.

jods
  • 4,581
  • 16
  • 20
1

I think your title and question are misleading, you might actually be looking for params.

For example if you redefine you constructor like:

public Player(string ID, params Languages[] languages)
{
   Languages = languages.ToList();
}

You can pass the languages you support as a list of parameters

new Player("Player A", Languages.Ger, Languages.Eng)
Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
0
Enum.GetValues(typeof(Languages)).Cast<Languages>();
Ron.B.I
  • 2,726
  • 1
  • 20
  • 27
0

Your question:

How to call the constructor with the list of languages?

The sleekest possible way:

E.g. you have a player who can speak english and german:

Player PlayerA = new Player("Player A", new List<Languages> 
{ Languages.Eng, Languages.Ger });
Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
0

If you do not accept to modify class Player, just call the constructor like this:

new Player("Player A", new List<Languages> { Languages.Ger, Languages.Eng });

If you do accept to modify class Player, use params and change type of Languages to Languages[]:

public enum Languages
{
    Ger, Eng, Fra, Ita, Rus
}

public class Player
{
    public string ID { get; private set; }
    private Languages[] Languages;

    public Player(string ID, params Languages[] LangList)
    {
        this.ID = ID;
        this.Languages = LangList;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Player PlayerA = new Player("Player A", Languages.Ger, Languages.Eng);
    }
}
lightbricko
  • 2,649
  • 15
  • 21