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?

  1. ref has nothing example. without ref pass reference of list, intended (and shared both forms). (code ref keyword needed - rare code.)

  2. list<t> has no notification changes. when change contents in 1 form, other 1 doesn't know it. consider using observablecollection<t> , subscribe collectionchanged event in controls.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -