1

I'm studying C# Classes and am trying to create a program that has a Class called Employee and derived classes of ProductionWorker, ShiftSupervisor, and TeamLeader.

I have a list box where I want to display All the employees, and within the program, there's functionality to add, edit, or remove respective people, but rather than making 3 lists like so:

List<ProductionWorkers> pWorkers = new List<ProductionWorkers>();
List<ShiftSupervisor> sSupervisors = new List<ShiftSupervisor>();
List<TeamLeader> tLeaders = new List<TeamLeader>();

I'd like to be able to have the Employee base class have or contain some sort of list of it's derived classes and their objects.

For example I'd like to be able to be able to Add and Remove derived objects to a list of Employees in some fashion, given the following example:

List<Employee> employees = new List<Employee>();
ProductionWorker _pWorker = new ProductionWorker();
_pWorker.Name = "Bob";
_pWorker.EmployeeID = 1234;
employees.Add(_pWorker));

I don't know if that's even possible or realistic to do that, but it would seem maybe there is a way from what I've read, I'm just not sure how to implement it. I'm open to better suggestions however, if someone knows of a better or proper way to get all the Employees listed into a ListBox without having to cycle through 3 different lists of the different derived classes.

For clarity, below is the Base class, then its following derived classes.

public class Employee
{
    public string Name { get; set; }
    public int EmployeeNumber { get; set; }
}

class ProductionWorker : Employee
{
    public int ShiftNumber { get; set; }
    public decimal HourlyPayRate { get; set; }
}

class TeamLeader : ProductionWorker
{
    public int ReqHours { get; set; }
    public int AttendedHours { get; set; }
}

class ShiftSupervisor : Employee
{
    public int Salary { get; set; }
    public int AnnualProductionBonus { get; set; }
}

I didn't realize until I posted my classes here that Team Leader is actually a derived class of Production Worker. I'm not sure if that changes things...

5
  • can you show the Class definition of the 3 classes that you have.. you can accomplish this using nested classes as well.. Commented Sep 11, 2015 at 20:32
  • 1
    Yes, I'll add the 3 derived classes as well as the base class definitions. Commented Sep 11, 2015 at 20:33
  • Your example code (with my edit) will work, since all specialized classes derive from Employees. Look into using typeof(). Commented Sep 11, 2015 at 20:33
  • 1
    Have you tried employees.Add(_pWorker);? What you're asking sounds fairly standard, but that wrapping new ProductionWorker doesn't make sense. Commented Sep 11, 2015 at 20:33
  • employees.Add(new ProductionWorker(_pWorker)); this line is wrong since you already created an new instance a few lines above change it to employees.Add(_pWorker); Commented Sep 11, 2015 at 20:36

1 Answer 1

4

Yes, you can add Employee items and items deriving from Employee to the employees list.

List<Employee> employees = new List<Employee>();
ProductionWorker pWorker = new ProductionWorker {
    Name = "Bob",
    EmployeeID = 1234
};
employees.Add(pWorker);

If you want to display all these different kinds of employees in the same listbox, override the ToString method in these classes. The ListBox will automatically use it in order to display the items.

public class ProductionWorker : Employee
{
    public override string ToString()
    {
        return String.Format("{0} ({1}), production", Name, EmployeeID);
    }
}

You can assign the list of employees to the listbox like this

employeeListBox.DataSource = employees;

From the employees list you can access the members declared in Employee directly:

int id = employees[i].EmployeeID;

However; you need to cast, if you want to access members of derived types

int salary = 0;
var supervisor = employees[i] as ShiftSupervisor;
if (supervisor != null) {
   salary = supervisor.Salary;
}

If you know the type of an item in advance you can cast directly

int salary = ((ShiftSupervisor)employees[0]).Salary;
Sign up to request clarification or add additional context in comments.

1 Comment

I thought I had tried this process, but I must have been missing something, this indeed does work. Thank you.

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.