This is an employee class, in which I can add employees and then view them through console.
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> GetAllEmployee()
{
return Employees;
}
}
Similarly a Project class was created in which I would like to assign a Project to an employee. So , I created an instance of Employeess
public class Projectss
{
Employeess classA= new Employeess();
public List<Project> Projects { get; set; } = new List<Project>();
private List<Assign> Assigns { get; set; } = new List<Assign>();
public void AddProject(Project project)
{
Projects.Add(project);
}
public List<Project> GetAllProject()
{
return Projects;
}
public void Add(Assign assign)
{
var id = assign.EmpId;
var pid = assign.PID;
//copied code
var emp = classA.Employees.Find(a => a.EmpId == id);
var prjct = Projects.Find(c => c.PID == pid);
if (emp != null && prjct != null)
{
Assigns.Add(assign);
}
}
public List<Assign> GetAllAssignedProject()
{
return Assigns;
}
}
The problem comes when executing the if statement. While the variable prjct
can be found but emp
always show null value as Employees show 0 count while debugging. I think this happens because I am using an instance of the class, so when I search an employee in the list Employees by its EmpId, it shows null value. So, how do I attempt to use the Employees in project class so that emp
is not null?
CodePudding user response:
Add some Employee
items before using it.
Employeess classA= new Employeess();
classA.AddEmployee(new Employee(x,y,z...)) // Call Employee constructor.
classA.AddEmployee(new Employee(x,y,z...))
....
Then
var emp = classA.GetAllEmployee().Find(a => a.EmpId == id);
CodePudding user response:
You dont want Employees inside Project. It needs to be separated. You can have Employees in different projects. The assignment should contain the employee before you call Add for the project.
SO you would do someting like this to assign emp1 to projectx
var emp = Employees.Lookup("idemp1");
var asg = new Assign{ Emp = emp, Date=....., etc};
projX.Add(asg);