Reputation: 315
I'm fighting with some classes or better objects taht are nested in a "main object".
Example:
public class Parent
{
public string MothersName { get; set; }
public Child child = new Child(){ ChildsName = "Child1"; }
}
public class Child
{
public string ChildsName{ get; set; }
.... Parent MothersName = "LovelyOne";
}
So is there any way to change the mothers name from the child-object?
Upvotes: 1
Views: 88
Reputation: 172200
The child needs to have a reference to the parent class:
public class Parent
{
public string MothersName { get; set; }
public Child child;
public Parent()
{
child = new Child("Child1", this);
}
}
public class Child
{
public string ChildsName { get; set; }
public Parent Parent { get; }
public Child(string childsName, Parent parent)
{
this.ChildsName = childsName;
this.Parent = parent;
}
}
myChild.Parent.MothersName = "LovelyOne";
works now inside of Child.
Upvotes: 3