简体   繁体   English

C# Visual Studio 2017 中的 System.StackOverflowException

[英]System.StackOverflowException in C# Visual Studio 2017

I am getting the following error:我收到以下错误:

System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown. System.StackOverflowException: 'System.StackOverflowException' 类型的异常被抛出。

The error takes place under the program where employee1 , employee2 , and employee3 are declared.错误发生在声明了employee1employee2employee3的程序下。 Everything else seems to be working fine, but no way to finding out since the program won't run with the current error.其他一切似乎都运行良好,但无法找出原因,因为程序不会以当前错误运行。 Their are naming messages in the class section, but I haven't gotten the incentive to fix those easy problem because of the systemoverflow error ;他们在类部分命名消息,但由于系统systemoverflow error ,我没有动力去解决那些简单的问题; additionally, the program still tends to run even with those messages.此外,即使有这些消息,程序仍然倾向于运行。 For Example: public string name should be public string Name/Department/IDNumber/Position .例如:公共字符串名称应该是public string Name/Department/IDNumber/Position

Class:班级:

 public partial class Employme
{
    private string _name;
    private decimal _idNumber;
    private string _department;
    private string _position;


    public Employme(string name, decimal idNumber, string department, string position)

    {
        _name = name;
        _idNumber = idNumber;
        _department = department;
        _position = position;
    }

    public Employme(string name, decimal idNumber)

    {
        _name = name;
        _idNumber = idNumber;
        _department = "";
        _position = "";
    }

    public string name
    {
        set { _name = value; }
        get { return _name; }
    }

    public decimal idNumber
    {
        set { _idNumber = value; }
        get { return _idNumber; }
    }

    public string department
    {
        set { _department = value; }
        get { return _department; }
    }

    public string position
    {
        set { _position = value; }
        get {  return _position ;}
    }

}

Program:程序:

public partial class Employme : Form
{
    private Employme employee1 = new Employme("Susan Myers", 47899, "Accounting", "Vice President");
    private Employme employee2 = new Employme("Mark Jones", 39119, "IT", "Programmer");
    private Employme employee3 = new Employme("Joy Rogers", 81774, "Manufacturing", "Engineer");

    public Employme()
    {
        _name = "";
        _idNumber = 0;
        _department = "";
        _position = "";

        InitializeComponent();
    }

    private void Employme_Load(object sender, EventArgs e)
    {

    }

    private void displayButton_Click(object sender, EventArgs e)
    {

        susanTextBox.Text = employee1.name + "," + employee1.idNumber + "," + employee1.department + "," + employee1.position;
        markTextBox.Text = employee2.name + "," + employee2.idNumber + "," + employee2.department + "," + employee2.position;
        joyTextBox.Text = employee3.name + "," + employee3.idNumber + "," + employee3.department + "," + employee3.position;
    }
}

Class inheriting from Form should not be named the same as the class from first snippet.从 Form 继承的类不应与第一个片段中的类命名相同。 As it is both snippets constitute same class, and everytime you call a constructor to create instance of Employme, you create 3 other instances of the same Employme while initializing private fields, calling constructor 3 more times before you reach code from your original constructor call, each of those 3 calls initializes more private fields, calling constructors more times, and so on, until you get the exception.由于这两个片段构成相同的类,并且每次调用构造函数来创建 Employme 的实例时,都会在初始化私有字段的同时创建同一个 Employme 的其他 3 个实例,在从原始构造函数调用获得代码之前,再调用构造函数 3 次,这 3 个调用中的每一个都会初始化更多私有字段,调用构造函数更多次,依此类推,直到出现异常。

To fix, rename one of classes.要修复,请重命名其中一个类。 If you rename class inheriting from Form, also remove assignments from its constructor.如果重命名从 Form 继承的类,还要从其构造函数中删除赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM