为什么我在 运行 这段代码时收到错误消息?
Why am I getting an error while running this code?
当我 运行 控制台应用程序时,我遇到了这个堆栈溢出错误。
由于此错误似乎出现在我的代码的 Assignlogic 部分,因此我记下了那部分代码和显示的错误。我的问题是如何在不改变代码功能的情况下处理这个异常?
//Assign
public class Assignlogic
{
private List<Assign> Assigns { get; set; } = new List<Assign>();//Here exception unhandled was thrown
//System.WhosebugException: 'Exception of type 'System.WhosebugException' was thrown.'
readonly Assignlogic logicC = new Assignlogic();
public void AddEmployeetoProject(Assign assign, Employeelogic logicA, Projectlogic logicB)
{
List<Employee> Employes = logicA.Employees;
List<Project> Projcts = logicB.Projects;
List<Assign> Assignss = logicC.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)
{
Assignss.Add(assign);
}
}
//view all assigned projects
public List<Assign> GetAllAssignedProjects()
{
return Assigns;
}
//remove an employee from a project
public void RemoveEmployee(string id)
{
var emp = Assigns.Find(a => a.EmpId == id);
if (emp != null)
{
Assigns.Remove(emp);
}
}
public bool SearchProjectbyMappedId(string id)
{
var employee = Assigns.Find(c => c.EmpId == id);
if (employee != null)
{
return true;
}
else
{
return false;
}
}
}
创建 Assignlogic
的实例时会发生什么?这个:
readonly Assignlogic logicC = new Assignlogic();
所以创建一个 Assignlogic
的实例会创建一个 Assignlogic
的实例,它会创建一个 Assignlogic
的实例,它会创建一个 Assignlogic
的实例,等等,等等
我不知道你的意图在这里,但这显然不是这样做的方法。对象不应该无限递归地创建自己。
您的 class AssignLogic
中有此成员
readonly Assignlogic logicC = new Assignlogic();
因此,当您创建一个 AssignLogic 时,它必须去创建一个 AssignLogic 以放置在那里。创建那个 AssignLogic 需要另一个 AssignLogic,.......
当我 运行 控制台应用程序时,我遇到了这个堆栈溢出错误。 由于此错误似乎出现在我的代码的 Assignlogic 部分,因此我记下了那部分代码和显示的错误。我的问题是如何在不改变代码功能的情况下处理这个异常?
//Assign
public class Assignlogic
{
private List<Assign> Assigns { get; set; } = new List<Assign>();//Here exception unhandled was thrown
//System.WhosebugException: 'Exception of type 'System.WhosebugException' was thrown.'
readonly Assignlogic logicC = new Assignlogic();
public void AddEmployeetoProject(Assign assign, Employeelogic logicA, Projectlogic logicB)
{
List<Employee> Employes = logicA.Employees;
List<Project> Projcts = logicB.Projects;
List<Assign> Assignss = logicC.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)
{
Assignss.Add(assign);
}
}
//view all assigned projects
public List<Assign> GetAllAssignedProjects()
{
return Assigns;
}
//remove an employee from a project
public void RemoveEmployee(string id)
{
var emp = Assigns.Find(a => a.EmpId == id);
if (emp != null)
{
Assigns.Remove(emp);
}
}
public bool SearchProjectbyMappedId(string id)
{
var employee = Assigns.Find(c => c.EmpId == id);
if (employee != null)
{
return true;
}
else
{
return false;
}
}
}
创建 Assignlogic
的实例时会发生什么?这个:
readonly Assignlogic logicC = new Assignlogic();
所以创建一个 Assignlogic
的实例会创建一个 Assignlogic
的实例,它会创建一个 Assignlogic
的实例,它会创建一个 Assignlogic
的实例,等等,等等
我不知道你的意图在这里,但这显然不是这样做的方法。对象不应该无限递归地创建自己。
您的 class AssignLogic
中有此成员 readonly Assignlogic logicC = new Assignlogic();
因此,当您创建一个 AssignLogic 时,它必须去创建一个 AssignLogic 以放置在那里。创建那个 AssignLogic 需要另一个 AssignLogic,.......