为什么我得到异常“system . stackoverflowexception”?
本文关键字:system stackoverflowexception 异常 为什么 | 更新日期: 2023-09-27 17:53:37
我有这个代码。代码从表、表内容、一些文本框和其他东西中获取一些值。当我点击提交按钮,我得到一个值,并放入"st"(类型类学生),并放入数据库。但它向我展示了列表属性中的一个异常"get {....}" the exception "StackOverflowException "
public StudentManager()
: base(ConfigurationManager.ConnectionStrings["con"].ConnectionString)
{
}
public override void Add(Student entity)
{
//add to database
}
protected void submitButton_Click(object sender, EventArgs e)
{
Student st = new Student();
st.id = Convert.ToInt32(IdTextBox.Text);
st.AVG = Convert.ToDouble(AVGTextBox.Text);
st.date = dateCalendar.TodaysDate;
st.educationInfo = educationInfoTextBox.Text;
faculty fa = new faculty();
fa.id = Convert.ToInt32(facultyDropDownList.SelectedValue);
st.faculty = fa;
st.fatherName = fatherNameTextBox.Text;
st.fName = fNameTextBox.Text;
st.lName = lNameTextBox.Text;
st.motherName = motherNameTextBox.Text;
st.password = passwordTextBox.Text;
st.personalInfo = personalInfoTextBox.Text;
StudentManager sm = new StudentManager();
sm.Add(st);
}
public class Student
{
public int id { get; set; }
public faculty faculty { get; set; }
public double AVG { get; set; }
public DateTime date { get; set; }
public string educationInfo { get; set; }
public string fatherName { get; set; }
public string fName { get; set; }
public string lName { get; set; }
public string motherName { get; set; }
public string password { get; set; }
public string personalInfo { get; set; }
private List<SqlParameter> Attributes;
public List<SqlParameter> attributes
{
get
{
Attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id",this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo",educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return Attributes;
}
}
}
您的attributes_Get
方法递归地调用自己。
改为:
// this should be a Get() method, not a property.
public List<SqlParameter> GetAttributes()
{
attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id",this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo",educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return attributes;
}
这是因为你的类中的属性进行了递归调用。
public List<SqlParameter> attributes
{
get
{
Attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id",this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo",educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return Attributes;
}
要解决这个问题,我觉得最好将其转换为方法而不是使用属性。可以这样做:
public List<SqlParameter> GetAttributes()
{
//replace your code here to get attributes
List<SqlParameter> attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id", this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo", educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return attributes;
}
从代码中删除private List<SqlParameter> Attributes;
因为当你的代码调用这个属性时:
public List<SqlParameter> attributes
{
get
{
Attributes = new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
attributes.Add(new SqlParameter("id",this.id));
attributes.Add(new SqlParameter("faculty", this.faculty));
attributes.Add(new SqlParameter("AVG", this.AVG));
attributes.Add(new SqlParameter("date", date));
attributes.Add(new SqlParameter("educationInfo",educationInfo));
attributes.Add(new SqlParameter("fatherName", fatherName));
attributes.Add(new SqlParameter("lName", lName));
attributes.Add(new SqlParameter("motherName", motherName));
attributes.Add(new SqlParameter("password", password));
attributes.Add(new SqlParameter("personalInfo", personalInfo));
return Attributes;
}
}
到达attributes.Add()
行再次调用这个属性这是递归调用所以使用另一个变量名,例如
public List<SqlParameter> attributes
{
get
{
var myAttributes= new List<SqlParameter>();
SqlParameter sp = new SqlParameter();
myAttributes.Add(new SqlParameter("id",this.id));
myAttributes.Add(new SqlParameter("faculty", this.faculty));
myAttributes.Add(new SqlParameter("AVG", this.AVG));
myAttributes.Add(new SqlParameter("date", date));
myAttributes.Add(new SqlParameter("educationInfo",educationInfo));
myAttributes.Add(new SqlParameter("fatherName", fatherName));
myAttributes.Add(new SqlParameter("lName", lName));
myAttributes.Add(new SqlParameter("motherName", motherName));
myAttributes.Add(new SqlParameter("password", password));
myAttributes.Add(new SqlParameter("personalInfo", personalInfo));
return myAttributes;
}
}