簡體   English   中英

C# Visual Studio 2017 中的 System.StackOverflowException

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

我收到以下錯誤:

System.StackOverflowException: 'System.StackOverflowException' 類型的異常被拋出。

錯誤發生在聲明了employee1employee2employee3的程序下。 其他一切似乎都運行良好,但無法找出原因,因為程序不會以當前錯誤運行。 他們在類部分命名消息,但由於系統systemoverflow error ,我沒有動力去解決那些簡單的問題; 此外,即使有這些消息,程序仍然傾向於運行。 例如:公共字符串名稱應該是public string Name/Department/IDNumber/Position

班級:

 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 ;}
    }

}

程序:

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;
    }
}

從 Form 繼承的類不應與第一個片段中的類命名相同。 由於這兩個片段構成相同的類,並且每次調用構造函數來創建 Employme 的實例時,都會在初始化私有字段的同時創建同一個 Employme 的其他 3 個實例,在從原始構造函數調用獲得代碼之前,再調用構造函數 3 次,這 3 個調用中的每一個都會初始化更多私有字段,調用構造函數更多次,依此類推,直到出現異常。

要修復,請重命名其中一個類。 如果重命名從 Form 繼承的類,還要從其構造函數中刪除賦值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM