Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Practices Refering to Parent Object

Status
Not open for further replies.

elconomeno

Technical User
Jun 24, 2004
24
BE
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();
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top