2013-01-16 175 views
0

我有這段代碼。代碼從表格,表格內容,一些文本框和其他東西中獲取一些值。當我點擊按鈕提交按鈕時,我得到一個值,並輸入「st」(輸入class student)並放入數據庫。但它顯示了我一個例外列表屬性「得到 {....}」例外「System.StackOverflowException」爲什麼我得到異常「System.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; 
      } 

     } 

    } 
+0

請注意,通過約定屬性開頭的大寫字母和fi領域以小寫字母開頭。 – Servy

+0

你打算在你的「getter」中調用'Attributes.Add'而不是'attributes.Add'嗎? –

+0

@SamIam好吧,這將是一個很奇怪的,有一個吸氣劑,像這樣的突變... – Servy

回答

2

這是因爲在你班上特性使得遞歸調用。

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

+1

這是一個非常有趣的「吸氣劑」 –

+0

@SamIam - 而不是財產方法解決問題,我想這樣... –

+0

將其轉換爲方法不是真正的解決方案;他只是不需要遞歸地調用自己的... – Servy

3

attributes_Get方法遞歸調用自身從您的代碼刪除private List<SqlParameter> Attributes;

將其更改爲:

// 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; 
    } 
+0

他還需要也可以改變字段,或者將'屬性'設爲本地。 – Servy

+0

噢,你不應該把'Get'前綴加到一個屬性上;它沒有任何意義。 – Servy

+0

@Servy同意 - 我只是忘了添加包裝:) –

0

,因爲當你的代碼調用此屬性:

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

    } 
相關問題