为什么我在运行此代码时出现错误?

发布于 2025-01-09 00:23:17 字数 1801 浏览 0 评论 0原文

当我运行控制台应用程序时,出现堆栈溢出错误。 由于此错误似乎出现在我的代码的分配逻辑部分,因此我写下了该部分代码和显示的错误。我的问题是如何在不改变代码功能的情况下处理这个异常?

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2025-01-16 00:23:17

创建 Assignlogic 实例时会发生什么?这样:

readonly Assignlogic logicC = new Assignlogic();

因此,创建 Assignlogic 的实例会创建一个 Assignlogic 的实例,该实例会创建一个 Assignlogic 的实例,而该实例又会创建一个 的实例 我不知道你的意图是什么

,但这显然不是这样做的方法。对象不应该无限地递归地创建自己。

What happens when you create an instance of Assignlogic? This:

readonly Assignlogic logicC = new Assignlogic();

So creating an instance of Assignlogic creates an instance of Assignlogic, which creates an isntance of Assignlogic, which creates an instance of Assignlogic, 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.

三生一梦 2025-01-16 00:23:17

你的AssignLogic类中有这个成员,

    readonly Assignlogic logicC = new Assignlogic();

所以当你创建一个AssignLogic时,它必须创建一个AssignLogic来放在那里。创建该AssignLogic需要另一个AssignLogic,......

you have this member in your class AssignLogic

    readonly Assignlogic logicC = new Assignlogic();

So when you create an AssignLogic, it has to go and create an AssignLogic to put there. Creating that AssignLogic requires another AssignLogic,.......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文