Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with assignment using properties... maybe because of IClonable interface

Problem with assignment using properties... maybe because of IClonable interface

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fordge
    wrote on last edited by
    #1

    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 property stud.Name.FirstName = "zai"; while the one below is ok stud.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?

    S 1 Reply Last reply
    0
    • F fordge

      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 property stud.Name.FirstName = "zai"; while the one below is ok stud.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?

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      stud.Name.FirstName = "zai"; doesn't work because of the getter for the Name 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

      www.troschuetz.de

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups