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.

2

6 Answers 6

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)

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

Comments

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.

Comments

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)

Comments

0
Enum.GetValues(typeof(Languages)).Cast<Languages>();

1 Comment

He doesn't want all languages.
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 });

Comments

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.