c# - When exporting to xml file receiving error infinite loop -
this question has answer here:
i using c# on visual studio 2015 create program assignment. created class teacher information, each variable have getter , setter.
public class teacher { public string teacherid { { return teacherid; } set { teacherid = value; } } public string teachername { { return teachername; } set { teachername = value; } } public string teacheraddress { { return teacheraddress; } set { teacheraddress = value; } } public string teacherdob { { return teacherdob; } set { teacherdob = value; } } public string teachercontact { { return teachercontact; } set { teachercontact = value; } } public string teachersalary { { return teachersalary; } set { teachersalary = value; } } public string class1 { { return class1; } set { class1 = value; } } public string class2 { { return class2; } set { class2 = value; } } public string class3 { { return class3; } set { class3 = value; } } public string class4 { { return class4; } set { class4 = value; } } public string class5 { { return class5; } set { class5 = value; } } }
i created class names savexml export data text boxes xml file.
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(); } }
i created form text box every variable , set save button code below export information xml file.
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); } }
the program runs fine when input data , press save button program freezes , receive error below.
an unhandled exception of type 'system.stackoverflowexception' occurred in userregistration.exe
i trying find issue till can't find problem, great. thanks
look @ property in class:
public string teacherid { { return teacherid; } set { teacherid = value; } }
it references itself.
change to:
public string teacherid { get; set; }
or:
private string teacherid; public string teacherid { { return teacherid; } set { teacherid= value; } }
Comments
Post a Comment