c# - What am I missing about reference types in this example? -
i have following code in create object , pass along through series of forms user control.
public class myobject { public list<anobject> objects {get; set;} }
form 1:
private void myobject _myobject = new myobject{objects = new list<anobject>()}; ... form2 form2 = new form2(ref _myobject); form2.show();
form 2:
public form2(ref myobject myobject) { usercontrol1 mycontrol = new usercontrol1(); mycontrol.objects = myobjects.objects }
usercontrol1
public list<anobject> objects {get; set;} ... objects.add(new myobject());
when add new myobject() objects in usercontrol1, doesn't update original list on form1. passing myobject reference. thing can think of somehow unboxing values when assign list of anobject form 2 usercontrol1. doing wrong?
ref has nothing example. without ref pass reference of list, intended (and shared both forms). (code ref keyword needed - rare code.)
list<t>
has no notification changes. when change contents in 1 form, other 1 doesn't know it. consider usingobservablecollection<t>
, subscribecollectionchanged
event in controls.
Comments
Post a Comment