elconomeno
Technical User
These classes are examples of how I make a parent-child reference between objects.
But are these classes a good approach to solve the problem?
are there shorter ways to accomplish?
SOLUTION WITH DELEGATES
/// <summary>
/// Parrent Object
/// </summary>
public class Parent : ObservableCollection<child>
{
public string Name;
public new void AddChild(string name)
{
child NewItem = new child();
NewItem.OtherParameter = name;
NewItem.GetParentEvent += new Func<Parent>(item_GetParentEvent);
base.Add(NewItem);
}
Parent item_GetParentEvent() { return this; }
}
/// <summary>
/// Child Object
/// </summary>
public class child
{
public string OtherParameter;
private event Func<Parent> getParentEvent;
public event Func<Parent> GetParentEvent
{// First try to remove the handler, then re-add it
add
{
getParentEvent -= value;
getParentEvent += value;
}
remove{getParentEvent -= value;}
}
private Parent _ParentItem;
[System.Xml.Serialization.XmlIgnoreAttribute]
public Parent ParentItem
{
get{GetParrent();
return _ParentItem;}
set{_ParentItem = value;}
}
private void GetParrent()
{
if (getParentEvent != null){ParentItem = getParentEvent();}
}
public class FamilyClass
{
Parent MyDad = new Parent();
Parent MyMam = new Parent();
public void ExecuteTime()
{
MyDad.Name = "My Daddy";
MyDad.AddChild("Me myself and i");
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyDad.Name = "My Daddy's split personality";
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyMam.Name = "My Mam";
child MySelf = MyDad.ElementAt(0);
MySelf.GetParentEvent += new Func<Parent>(MySelf_GetParentEvent);
Console.WriteLine(MySelf.ParentItem.Name);
Console.ReadLine();
}
Parent MySelf_GetParentEvent()
{
return MyMam;
}
}
static void Main(string[] args)
{
FamilyClass bb = new FamilyClass();
bb.ExecuteTime();
}
SOLUTION WITH CONTSTRUCTOR
public class Parent : ObservableCollection<child>
{
public string Name;
public new void Add(string name)
{
child NewItem = new child(this) { OtherParameter = name };
base.Add(NewItem);
}
}
public class child
{
public child(Parent MyParentObject)
{
_ParentItem = MyParentObject;
}
readonly Parent _ParentItem;
public string OtherParameter;
public Parent ParentItem
{
get { return _ParentItem; }
}
}
static void Main(string[] args)
{
Parent MyDad = new Parent();
MyDad.Name = "My dad";
MyDad.Add("Me");
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyDad.Name = "My Daddy's split personality";
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
Console.ReadLine();
}
But are these classes a good approach to solve the problem?
are there shorter ways to accomplish?
SOLUTION WITH DELEGATES
/// <summary>
/// Parrent Object
/// </summary>
public class Parent : ObservableCollection<child>
{
public string Name;
public new void AddChild(string name)
{
child NewItem = new child();
NewItem.OtherParameter = name;
NewItem.GetParentEvent += new Func<Parent>(item_GetParentEvent);
base.Add(NewItem);
}
Parent item_GetParentEvent() { return this; }
}
/// <summary>
/// Child Object
/// </summary>
public class child
{
public string OtherParameter;
private event Func<Parent> getParentEvent;
public event Func<Parent> GetParentEvent
{// First try to remove the handler, then re-add it
add
{
getParentEvent -= value;
getParentEvent += value;
}
remove{getParentEvent -= value;}
}
private Parent _ParentItem;
[System.Xml.Serialization.XmlIgnoreAttribute]
public Parent ParentItem
{
get{GetParrent();
return _ParentItem;}
set{_ParentItem = value;}
}
private void GetParrent()
{
if (getParentEvent != null){ParentItem = getParentEvent();}
}
public class FamilyClass
{
Parent MyDad = new Parent();
Parent MyMam = new Parent();
public void ExecuteTime()
{
MyDad.Name = "My Daddy";
MyDad.AddChild("Me myself and i");
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyDad.Name = "My Daddy's split personality";
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyMam.Name = "My Mam";
child MySelf = MyDad.ElementAt(0);
MySelf.GetParentEvent += new Func<Parent>(MySelf_GetParentEvent);
Console.WriteLine(MySelf.ParentItem.Name);
Console.ReadLine();
}
Parent MySelf_GetParentEvent()
{
return MyMam;
}
}
static void Main(string[] args)
{
FamilyClass bb = new FamilyClass();
bb.ExecuteTime();
}
SOLUTION WITH CONTSTRUCTOR
public class Parent : ObservableCollection<child>
{
public string Name;
public new void Add(string name)
{
child NewItem = new child(this) { OtherParameter = name };
base.Add(NewItem);
}
}
public class child
{
public child(Parent MyParentObject)
{
_ParentItem = MyParentObject;
}
readonly Parent _ParentItem;
public string OtherParameter;
public Parent ParentItem
{
get { return _ParentItem; }
}
}
static void Main(string[] args)
{
Parent MyDad = new Parent();
MyDad.Name = "My dad";
MyDad.Add("Me");
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
MyDad.Name = "My Daddy's split personality";
Console.WriteLine(MyDad.ElementAt(0).ParentItem.Name);
Console.ReadLine();
}