Issue
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
In your public void Add(Assign assign) function you make classA and classB, these are new objects of this class and have no values in them. If you want to use the function like this you could make the function like this: public void Add(Assign assing, Employeeclass classA, Projectclass classB) ... And then you can give your existing object with there value with them.
Answered By - Jorne Schuurmans Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.