Problem with assignment using properties... maybe because of IClonable interface
-
Consider the following code example
class Program { static void Main(string[] args) { StudentInfo stud = new StudentInfo(); stud.Name.FirstName = "zai"; //fails assigns firstname as null stud.name.FirstName = "zai"; //works succesfully } } class NameInfo :ICloneable { private string firstname; public string FirstName { get { return firstname; } set { firstname = value; } } public object Clone() { NameInfo n = new NameInfo(); n.LastName = this.lastname; return n; } } class StudentInfo:ICloneable { public NameInfo name = new NameInfo(); public NameInfo Name { get { if (name != null) return (NameInfo)name.Clone(); else return null; } set { if (value != null) name = (NameInfo)value.Clone(); else name = null; } } public object Clone() { StudentInfo s = new StudentInfo(); s.Name = this.Name; return s; } }
the following statement fails when accessing firstname thru Name propertystud.Name.FirstName = "zai";
while the one below is okstud.name.FirstName = "zai";
why?? if I was not using an IClonable interface both of them work....but im not sure its an IClonable issue... is there any rule that says that we souldnt access inner variables through a property of that object? -
Consider the following code example
class Program { static void Main(string[] args) { StudentInfo stud = new StudentInfo(); stud.Name.FirstName = "zai"; //fails assigns firstname as null stud.name.FirstName = "zai"; //works succesfully } } class NameInfo :ICloneable { private string firstname; public string FirstName { get { return firstname; } set { firstname = value; } } public object Clone() { NameInfo n = new NameInfo(); n.LastName = this.lastname; return n; } } class StudentInfo:ICloneable { public NameInfo name = new NameInfo(); public NameInfo Name { get { if (name != null) return (NameInfo)name.Clone(); else return null; } set { if (value != null) name = (NameInfo)value.Clone(); else name = null; } } public object Clone() { StudentInfo s = new StudentInfo(); s.Name = this.Name; return s; } }
the following statement fails when accessing firstname thru Name propertystud.Name.FirstName = "zai";
while the one below is okstud.name.FirstName = "zai";
why?? if I was not using an IClonable interface both of them work....but im not sure its an IClonable issue... is there any rule that says that we souldnt access inner variables through a property of that object?stud.Name.FirstName = "zai";
doesn't work because of the getter for theName
property. There you clone the field name and return this newly created instance. Afterwards you assign a new first name to the clone instance, which cannot reflect in the field of your StudentInfo instance as your operating on another instance of Nameinfo. So if you clone a field inside a getter before returning it, you actually protect it from be changed by any outside code.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook