Reputation: 1
I am getting the following error:
System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.
The error takes place under the program where employee1
, employee2
, and employee3
are declared. 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
; additionally, the program still tends to run even with those messages. For Example: public string name should be 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;
}
}
Upvotes: 0
Views: 392
Reputation: 55
Class inheriting from Form should not be named the same as the class from first snippet. 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.
To fix, rename one of classes. If you rename class inheriting from Form, also remove assignments from its constructor.
Upvotes: 2