How to assign events

  • Thread starter Norbert Pürringer
  • Start date
N

Norbert Pürringer

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
 
M

Marc Gravell

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;
}
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

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

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top