Monday 15 September 2014

c# - Can not reference a type through an expression why? -



c# - Can not reference a type through an expression why? -

i trying create 2 forms.

in form 1 want save new contacts, can display them later, , adding sec button opening form 2 want create contact , after closing window save contact in list created in form 1. getting error:

can not reference type through look

on f2.contacts = this.contacts; , don't know why.

form 1:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsformsapplication1 { public partial class form1 : form { public contacts contacts = new contacts(); public form1() { initializecomponent(); } public class contacts { private list<contacts> people = new list<contacts>(); public list<contacts> people { { homecoming people; } } } private void button1_click(object sender, eventargs e) { form2 f2 = new form2(); f2.contacts = this.contacts; f2.show(); } } }

form 2:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsformsapplication1 { public partial class form2 : form { public class contacts { private list<person> persons = new list<person>(); public list<person> persons { { homecoming this.persons; } } } public contacts contacts { get; set; } public form2() { initializecomponent(); } private void button1_click(object sender, eventargs e) { person p = new person(); p.name = textbox1.text; p.lastname = textbox2.text; p.phonenumber = textbox3.text; p.email = textbox4.text; this.contacts.persons.add(p); } public class person { public string name { get; set; } public string lastname { get; set; } public string phonenumber { get; set; } public string email { get; set; } } } }

you (accidentally) referring nested contacts class. when use

f2.contacts = this.contacts;

you refer class form2.contacts.

but want refer form2.contacts property:

f2.contacts = this.contacts;

c#

No comments:

Post a Comment