'How to assign objects from two different classes?

I have two constructors Employee Employee(string id, string FirstName, string LastName) and ProjectProject(string PID, string PName) . To add an employee or view the whole project list, I have created a business logic :

   public class Employeeclass
    {
        public List<Employee> Employees { get; set; } = new List<Employee>();

        public void AddEmployee(Employee employee)
        {
            Employees.Add(employee);
        }

        public List<Employee> GetAllEmployee()
        {
            return Employees;
        }
}     
public class Projectclass
    {
        public List<Project> Projects { get; set; } = new List<Project>();

        public void AddProject(Project project)
        {
            Projects.Add(project);
        }

        public List<Project> GetAllProject()
        {
            return Projects;
        }
    }

In this business logic, I want to assign a project to an employee. For this I generated a separate constructor Assign(string EmpId, string PID)and created a class to assign the projects. First I add a employee and then a project. By using EmpId and PID, I want to assign the projects to employee. It was possible to do so when all the business logic was under a single class.

public class Assignclass
    {
        public List<Assign> Assigns { get; set; } = new List<Assign>();
        //assign employees to project
        public void Add(Assign assign)
        {
            var id = assign.EmpId;
            var pid = assign.PID;
            var emp = Employees.Find(a => a.EmpId == id);
            var prjct = Projects.Find(c => c.PID == pid);
            if (emp != null || prjct != null)
            {
                Assigns.Add(assign);
            }
        }
}

I could use this code when I have put Employeeclass, Projectclass and Assignclass as a single class. Now that I have segregated the classes in Employeeclass, Projectclass, Assignclass; I can't use the same code as Employees and Projects can't exist without context so I created reference variables for Employees and Projects.

public void Add(Assign assign)
        {
            Employeeclass classA = new Employeeclass();
            Projectclass classB = new Projectclass();
            
            List<Employee> Employes = classA.Employees;
            List<Project> Projcts = classB.Projects;
            //List<Assign> Assignss = classC.Assigns;
            var id = assign.EmpId;
            var pid = assign.PID;
            var emp = Employes.Find(a => a.EmpId == id);
            var prjct = Projcts.Find(c => c.PID == pid);
            if (emp != null || prjct != null)
            {
                Assigns.Add(assign);
            }
        }

But it shows while debugging that there are no elements in Employes. I can't use Find to find an employee with the same EmpId; same with Projects and Projcts. How should I assign the project to employees and what am I doing wrong?



Solution 1:[1]

This design looks like a database design. In C# it is easy to have relations, for example by a class simply having a property with type of another class.

I would just give a quick hint for a different design (instead of having an entity for the relation itself):

class Project
{
    public List<Employee> Employees { get; set; } = new List<Employee>();
    // (...) other properties
}

So when you want to assign an Employee to a project, you can simply do

Employee e = GetEmployee(...);
project.Employees.Add(e);

Does that help?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Károly Ozsvárt