Hello,

I've got a recursive data model, where the root object gets a certain
event handler:

DataObject root = new DataObject();
root.DataObjectCreated += new
DataObjectEventHandler(OnDataObjectCreated);

Now the DataObject has a collection of sub data objects with following
property:

public List<DataObject> DataObjects
{
get
{
if (_dataObjects == null)
{
_dataObjects = new List<DataObject>();
DataObject child = new DataObject();
_dataObjects.Add(child);
child.DataObjectCreated += DataObjectCreated /// ????
}
return _dataObjects;
}
}

How can I assign the event handler of the root to the event handler of
the child. My desire is, that a certain function is always called, if
a data object (never mind if root or child) is created.

How can I realize it?

Thank you,
Norbert

Re: How to assign events by Marc

Marc
Thu May 08 07:52:35 CDT 2008

Well, one option (other than lots and lots of events) would be to keep
the parent in each object, but this often gets tricky to maintain...
another option is to have a higher level object to do this (see below;
compares [for example] to how XmlNode or TreeNode work) - but note
that in this case I would probably not expose List<T> directly; I'd
want to intercept the Add etc so that children can only be added from
the same root (by subclassing Collection<T>, probably).

Marc

using System;
using System.Collections.Generic;

static class Program
{
static void Main()
{
DataRoot root = new DataRoot();
root.DataObjectCreated += new
EventHandler(root_DataObjectCreated);
DataObject obj = root.CreateDataObject();
DataObject desc =
obj.DataObjects[0].DataObjects[0].DataObjects[0];
}

static void root_DataObjectCreated(object sender, EventArgs e)
{
Console.WriteLine("root_DataObjectCreated");
}
}

public class DataRoot
{
public event EventHandler DataObjectCreated;
protected virtual void OnDataObjectCreated()
{
if (DataObjectCreated != null) DataObjectCreated(this,
EventArgs.Empty);
}
public DataObject CreateDataObject()
{
DataObject obj = new DataObject(this);
OnDataObjectCreated();
return obj;
}
}

public class DataObject
{
private readonly DataRoot root;
public DataRoot Root { get { return root; } }

internal DataObject(DataRoot root)
{
if (root == null) throw new ArgumentNullException("root");
this.root = root;
}

private List<DataObject> _dataObjects;
public List<DataObject> DataObjects
{
get
{
if (_dataObjects == null)
{
_dataObjects = new List<DataObject>();
DataObject child = root.CreateDataObject();
_dataObjects.Add(child);
}
return _dataObjects;
}
}
}

Re: How to assign events by Ignacio

Ignacio
Thu May 08 08:40:31 CDT 2008

On May 8, 8:32=A0am, Norbert P=FCrringer <thalio...@graffiti.net> wrote:
> Hello,
>
> I've got a recursive data model, where the root object gets a certain
> event handler:
>
> DataObject root =3D new DataObject();
> root.DataObjectCreated +=3D new
> DataObjectEventHandler(OnDataObjectCreated);
>
> Now the DataObject has a collection of sub data objects with following
> property:
>
> public List<DataObject> DataObjects
> {
> =A0 get
> =A0 {
> =A0 =A0 if (_dataObjects =3D=3D null)
> =A0 =A0 {
> =A0 =A0 =A0 _dataObjects =3D new List<DataObject>();
> =A0 =A0 =A0 DataObject child =3D new DataObject();
> =A0 =A0 =A0 _dataObjects.Add(child);
> =A0 =A0 =A0 child.DataObjectCreated +=3D DataObjectCreated =A0/// ????
> =A0 =A0 }
> =A0 =A0 return _dataObjects;
> =A0 }
>
> }
>
> How can I assign the event handler of the root to the event handler of
> the child. My desire is, that a certain function is always called, if
> a data object (never mind if root or child) is created.
>
> How can I realize it?
>
> Thank you,
> Norbert

Hi,

Do you have more than one of such structures?
If not, then you can create a static event and a static handler to
deal with it.
If you can have more than one, you could pass in the constructor a
reference to the root, in this case only one instance will deal with
the creation of all the struct. In this case you could also create
another class to deal with the event ( a la presenter)

Re: How to assign events by Peter

Peter
Thu May 08 13:25:29 CDT 2008

On Thu, 08 May 2008 05:32:26 -0700, Norbert Pürringer
<thalion77@graffiti.net> wrote:

> [...]
> How can I assign the event handler of the root to the event handler of
> the child. My desire is, that a certain function is always called, if
> a data object (never mind if root or child) is created.

Is it really necessary that you indirect the handler through the root? I
would think that Ignacio's suggestion of having a static event would allow
all of the class instances to just use the same event and thus same
handler. That seems to be what you're really after anyway.

If you must go through the root, then you could provide a protected
OnDataObjectCreated() method that actually raises the event, and which the
children objects can call on their parent when their own
OnDataObjectCreate() method is called. Of course, an object with no
parent (i.e the root) would simply raise the event without raising it on
their parent.

I'm a little confused about your DataObjects property adding a child
merely because the list of children itself has been created. But since
that doesn't seem to have anything to do with the actual question, I'll
leave it alone. :)

Pete