c# - Can not set cascading property while setting single property works fine -
following code not set property. nothing , @ debugging jumps ahead, f11
job.tdatareference.checkedout = false;
job of type tjob class build myself property:
public tdatareference tdatareference { { return new tdatareference(this.datareference); } set { this.tdatareference = value; } }
tdatareference class build myself property:
public bool checkedout { { return (datarow[checkedout].tostring() == "true"); } set { datarow[checkedout] = value.tostring(); } }
and , private member datarow of type datarow
what funny following code works fine , sets property:
tdatareference dr = job.tdatareference; dr.checkedout = false;
who can explain me.
update
many answers! understand problem. :)
tdatareference depends on value in this.datareference earlyest time tdatareference can set when this.datareference known. how think changing this:
private tdatareference tdatareference; public tdatareference tdatareference { { if(tdatareference == null | tdatareference.id != this.datareference) { this.tdatareference = new tdatareference(this.datareference); } return this.tdatareference } set { this.tdatareference = value; } }
and wil setting cascading property work then???
your property getter creates new instance every time call it. code:
job.tdatareference.checkedout = false; // here setting checkedout 1st instance of tdatareference job.tdatareference.checkedout = false; // here setting checkedout 2nd instance of tdatareference
is analogue of pseudocode:
var tdataref1 = new tdatareference(job.datareference); tdataref1.checkedout = false; var tdataref2 = new tdatareference(job.datareference); tdataref2.checkedout = false;
you shouldn't write property way, violates design guidelines. consider result of condition:
job.tdatareference == job.tdatareference // false, ooops...
to avoid situation, fix property such way:
public tdatareference tdatareference { { return tdatareference; } set { this.tdatareference = value; } } // field initialization optional here, can omit // or make lazy initialization in getter this: // return tdatareference ?? (tdatareference = new tdatareference(this.datareference)); private tdatareference tdatareference = new tdatareference(this.datareference);