如何在主列表中添加元素而不是创建的实例?
How to add element in main list instead of the instance created?
我创建了一个构造函数 Employee(string id, string firstname, string lastname, string roleid) 并添加了一个 class 库作为业务逻辑和一个控制台应用程序到 运行 程序。我要做的是在 Employees 列表中添加和查看元素。
//Logic
public class Domain
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> GetAllEmployee()
{
return Employees;
}
}
在控制台应用程序中,我为此创建了一个程序,但新元素没有添加到列表中,而是添加到我创建的实例中。
//Console App
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("PPM Project");
Console.WriteLine("1 Add employee");
Console.WriteLine("2 View Employee");
var userInput = Console.ReadLine();
var business1 = new Domain();
var allemployee = business1.GetAllEmployee();
while (true)
{
switch (userInput)
{
case "1":
Console.WriteLine("adding employee first name:");
var FName = Console.ReadLine();
Console.WriteLine("adding employee last name:");
var LName = Console.ReadLine();
Console.WriteLine("adding 3digit Role Id:");
var RoleId = Console.ReadLine();
Console.WriteLine("adding 4digit Employee Id:");
var EmpId = Console.ReadLine();
var newEmployee = new Employee(EmpId, FName, LName, RoleId);
business1.AddEmployee(newEmployee);
break;
case "2":
foreach (var employee in allemployee)
{
Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
}
break;
default:
Console.WriteLine("Select");
break;
}
Console.WriteLine("Select option");
userInput = Console.ReadLine();
}
}
}
代码 运行 正确,我可以添加和查看员工和项目,但项目或员工都没有添加到我在业务逻辑中声明的项目和员工列表中,而是在新实例中我创建使用。如何在列表中添加元素,应该修改哪部分代码?
您正在适当地向 Domain.Employees
添加新的 Employee
class 实例,实际问题出在您的第二个 switch
案例中。
在 2
的情况下,您正在迭代 allemployee
这是在开始时获取的,而不是每当用户输入 2
作为选项时每次都获取所有员工。
通过这种方式,您将在每次调用中将最新的员工记录存储在 allemployee
变量中。
public static class Program
{
public static void Main(string[] args)
{
//Your existing code goes here
//var allemployee = business1.GetAllEmployee(); //Don't call it here
while (true)
{
switch (userInput)
{
case "1":
//Your existing code goes here
break;
case "2":
var allemployee = business1.GetAllEmployee(); //Call GetAllEmployee inside case 2 not on top
foreach (var employee in allemployee)
{
Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
}
break;
//Your existing code goes here
}
}
}
}
我创建了一个构造函数 Employee(string id, string firstname, string lastname, string roleid) 并添加了一个 class 库作为业务逻辑和一个控制台应用程序到 运行 程序。我要做的是在 Employees 列表中添加和查看元素。
//Logic
public class Domain
{
public List<Employee> Employees { get; set; } = new List<Employee>();
public void AddEmployee(Employee employee)
{
Employees.Add(employee);
}
public List<Employee> GetAllEmployee()
{
return Employees;
}
}
在控制台应用程序中,我为此创建了一个程序,但新元素没有添加到列表中,而是添加到我创建的实例中。
//Console App
static class Program
{
static void Main(string[] args)
{
Console.WriteLine("PPM Project");
Console.WriteLine("1 Add employee");
Console.WriteLine("2 View Employee");
var userInput = Console.ReadLine();
var business1 = new Domain();
var allemployee = business1.GetAllEmployee();
while (true)
{
switch (userInput)
{
case "1":
Console.WriteLine("adding employee first name:");
var FName = Console.ReadLine();
Console.WriteLine("adding employee last name:");
var LName = Console.ReadLine();
Console.WriteLine("adding 3digit Role Id:");
var RoleId = Console.ReadLine();
Console.WriteLine("adding 4digit Employee Id:");
var EmpId = Console.ReadLine();
var newEmployee = new Employee(EmpId, FName, LName, RoleId);
business1.AddEmployee(newEmployee);
break;
case "2":
foreach (var employee in allemployee)
{
Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
}
break;
default:
Console.WriteLine("Select");
break;
}
Console.WriteLine("Select option");
userInput = Console.ReadLine();
}
}
}
代码 运行 正确,我可以添加和查看员工和项目,但项目或员工都没有添加到我在业务逻辑中声明的项目和员工列表中,而是在新实例中我创建使用。如何在列表中添加元素,应该修改哪部分代码?
您正在适当地向 Domain.Employees
添加新的 Employee
class 实例,实际问题出在您的第二个 switch
案例中。
在 2
的情况下,您正在迭代 allemployee
这是在开始时获取的,而不是每当用户输入 2
作为选项时每次都获取所有员工。
通过这种方式,您将在每次调用中将最新的员工记录存储在 allemployee
变量中。
public static class Program
{
public static void Main(string[] args)
{
//Your existing code goes here
//var allemployee = business1.GetAllEmployee(); //Don't call it here
while (true)
{
switch (userInput)
{
case "1":
//Your existing code goes here
break;
case "2":
var allemployee = business1.GetAllEmployee(); //Call GetAllEmployee inside case 2 not on top
foreach (var employee in allemployee)
{
Console.WriteLine($"Employee: {employee.EmpId}\t {employee.FName} {employee.LName}, is assigned a RoleID : {employee.RoleId}");
}
break;
//Your existing code goes here
}
}
}
}