2016-02-06 55 views
0

我在visual studio 2015上使用c#來創建一個任務的程序。我爲教師信息創建了一個類,每個變量都有一個getter和一個setter。當導出到xml文件接收錯誤無限循環

public class Teacher 
{ 

    public string TeacherID 
    { 
     get { return TeacherID; } 
     set { TeacherID = value; } 
    } 

    public string TeacherName 
    { 
     get { return TeacherName; } 
     set { TeacherName = value; } 
    } 

    public string TeacherAddress 
    { 
     get { return TeacherAddress; } 
     set { TeacherAddress = value; } 
    } 

    public string TeacherDob 
    { 
     get { return TeacherDob; } 
     set { TeacherDob = value; } 
    } 

    public string TeacherContact 
    { 
     get { return TeacherContact; } 
     set { TeacherContact = value; } 
    } 

    public string TeacherSalary 
    { 
     get { return TeacherSalary; } 
     set { TeacherSalary = value; } 
    } 

    public string Class1 
    { 
     get { return Class1; } 
     set { Class1 = value; } 
    } 

    public string Class2 
    { 
     get { return Class2; } 
     set { Class2 = value; } 
    } 

    public string Class3 
    { 
     get { return Class3; } 
     set { Class3 = value; } 
    } 

    public string Class4 
    { 
     get { return Class4; } 
     set { Class4 = value; } 
    } 

    public string Class5 
    { 
     get { return Class5; } 
     set { Class5 = value; } 
    } 
} 

我創建了另一個類名savexml將數據從文本框導出到xml文件。

public class SaveXML 
{ 
    public static void SaveData(object obj, string filename) 
    { 
     XmlSerializer sr = new XmlSerializer(obj.GetType()); 
     TextWriter writer = new StreamWriter(filename); 
     sr.Serialize(writer, obj); 
     writer.Close(); 
    } 

} 

我創建了一個形式的文本框爲每個變量和設置保存下面的代碼按鈕,以便將信息導出到XML文件。

 private void button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      Teacher teach = new Teacher(); 
      teach.TeacherID = textBox1.Text; 
      teach.TeacherName = textBox2.Text; 
      teach.TeacherAddress = textBox3.Text; 
      teach.TeacherDob = textBox4.Text; 
      teach.TeacherContact = textBox5.Text; 
      teach.TeacherSalary = textBox6.Text; 
      teach.Class1 = textBox7.Text; 
      teach.Class2 = textBox8.Text; 
      teach.Class3 = textBox9.Text; 
      teach.Class4 = textBox10.Text; 
      teach.Class5 = textBox11.Text; 
      SaveXML.SaveData(teach, "teacher.xml"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

該程序運行良好,但是當我輸入數據並按下保存按鈕程序凍結,我收到下面的錯誤。

An unhandled exception of type 'System.StackOverflowException' occurred in UserRegistration.exe 

我想找到了該問題可能是,但到現在爲止,我不能發現問題,任何幫助真的很大。由於

+1

我建議你閱讀如何在.NET中的屬性工作。你正在爲每個屬性設置無限遞歸。您特別要閱讀「自動屬性」和「帶後臺屬性」。 –

回答

4

看看在你的類的任何屬性:

public string TeacherID 
{ 
    get { return TeacherID; } 
    set { TeacherID = value; } 
} 

它引用到自身。

將其更改爲:

public string TeacherID { get; set; } 

或者:

private string teacherID; 
public string TeacherID 
{ 
    get { return teacherID; } 
    set { teacherID= value; } 
} 
+0

感謝隊友:)我試圖做同樣的事情,但一直得到一個錯誤......問題是,私人和公共變量之一有相同的名稱...解決了我的問題 –

1

的屬性以錯誤的方式來定義。

//Correct 
    public string Class3 
    { 
     get; set; 
    } 
    //Wrong 
    public string Class4 
    { 
     get { return Class4; } 
     set { Class4 = value; } 
    } 
0

getter函數中的行返回TeacherID以遞歸方式調用getter並導致堆棧溢出。對於二傳手同樣如此。所有的屬性都是一樣的。由於這個你得到了stackoverflow例外。