为什么我在运行此代码时出现错误?
当我运行控制台应用程序时,出现堆栈溢出错误。 由于此错误似乎出现在我的代码的分配逻辑部分,因此我写下了该部分代码和显示的错误。我的问题是如何在不改变代码功能的情况下处理这个异常?
//Assign
public class Assignlogic
{
private List<Assign> Assigns { get; set; } = new List<Assign>();//Here exception unhandled was thrown
//System.StackOverflowException: 'Exception of type 'System.StackOverflowException' 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;
}
}
}
When I was running a console application, I got this stack overflow error.
As this error seems to be in the Assignlogic part of my code, I have wrote down that part of code and the error which is shown. My question is how to handle this exception, without changing the functionality of code?
//Assign
public class Assignlogic
{
private List<Assign> Assigns { get; set; } = new List<Assign>();//Here exception unhandled was thrown
//System.StackOverflowException: 'Exception of type 'System.StackOverflowException' 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建
Assignlogic
实例时会发生什么?这样:因此,创建
Assignlogic
的实例会创建一个Assignlogic
的实例,该实例会创建一个Assignlogic
的实例,而该实例又会创建一个的实例 我不知道你的意图是什么
,但这显然不是这样做的方法。对象不应该无限地递归地创建自己。
What happens when you create an instance of
Assignlogic
? This:So creating an instance of
Assignlogic
creates an instance ofAssignlogic
, which creates an isntance ofAssignlogic
, which creates an instance ofAssignlogic
, etc., etc.I don't know what your intent is here, but this is clearly not the way to do it. Objects shouldn't recursively create themselves ad infinitum.
你的AssignLogic类中有这个成员,
所以当你创建一个AssignLogic时,它必须创建一个AssignLogic来放在那里。创建该AssignLogic需要另一个AssignLogic,......
you have this member in your class AssignLogic
So when you create an AssignLogic, it has to go and create an AssignLogic to put there. Creating that AssignLogic requires another AssignLogic,.......