1

I have created a list:

List<Employee> employees = new List<Employee();

I would like to achieve this:

Employee e1 = new Employee(Job.Employee, "Name", "C Sharp", "Oracle", "SQL");

I currently have this:

Employee e1 = new Employee(Job.Employee, "Name", Skills.CSharp);

This is the code inside the Employee class

public enum Job { Employee, Supervisor, Administrator };
public enum Skills {CSharp, SQL, PHP, Javascript, Web, Python, Oracle, CPlus, Perl };

protected Job job;
protected String employeeName;
protected String employeeName;

public Job Job
{
    get { return job; }
    protected set { job = value; }
}

public String EmployeeName
{
    get { return employeeName; }
    protected set { employeeName = value; }
}

public Skills Skills
{
    get {return skills; } 
}

I want to be able to enter as many 'skills' as I want as currently, only one skill can be entered in the Employee e1 as I have used a enum.

How would I put an array of 'skills' in the list/constructor?

2 Answers 2

6

You can use a params array parameter to pass variable number of Skills parameters to the constructor:

public Employee(Job job, string name, params Skills[] skills)

You will also have to modify the skills field to hold a collection of Skills instead of a single one. You can make it an array if you will not add/remove skills after the employee is created, or an IList<Skills> if you need to modify the collection.

For example:

public class Employee
{
    private List<Skills> _skills;       // skills stored as a private List 
                                        // to allow modification inside Employee class

    public Employee(Job job, string name, params Skills[] skills)
    {
        _skills = new List<Skills>(skills);
        ...
    }

    public IReadOnlyList<Skills> Skills // publicly visible as a read-only list
    {
        get { return _skills.AsReadOnly(); }
    }

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

5 Comments

And change the member variable and property to be a collection type.
My constructor is this: public Employee (Job Job, String Name, Skills Skills) { job = Job; name = Name; skills = Skills;}
@Jakub could you add some more detail on what you mean by modifying the skills field? Can you give me an example please?
@DannyWatson Sure. Check the edited answer for an example.
While I used the other persons answer in my code I believe your code was the most helpful and of course you put more effort into your solution for me. Thanks
3

An alternative to passing in a Skills[], you can change your enum to be a bit-mask of the skills that an employee has:

[Flags]
enum Skills
{
   None = 0,
   CSharp = 1 << 0,
   SQL = 1 << 1,
   PHP = 1 << 2,
   Javascript = 1 << 3,
   ...
}

Then the individual skills can be bitwise ORed together to create the skills that an individual employee has:

Employee e1 = new Employee(Job.Employee, "Name", Skills.CSharp | Skills.SQL | Skills.PHP );

Then to check if an employee has a specific skill, you can use Enum.HasFlag method, eg:

if( e1.Skills.HasFlag( Skills.CSharp ) )

1 Comment

Good solution, although it limits the number of skills to 32 (or 64 with long underlying type)

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.