determine object's collection

J

Jure Bogataj

Hello!

This may be a silly question, but anyway:

If I have collection / list of objects:

public class MyCollection : List<MyObject>
{
}

public class MyObject
{
...
}

And then somewhere in the code I add object to the collection:
....
MyCollection col = new MyCollection()
MyObject obj = new MyObject()
col.Add(obj);

Is it possible now to determine which is obj's parent collection (sth like:
obj.?ParentCollection?) or must I manually track parent collections (e.g.
new MyObject(col) or sth like that). I know object can participate in more
than one collection at the same time (or not) so this may not be possible at
all?


Thanks in advance!

Best regards,
Jure
 
J

Jon Skeet [C# MVP]

And then somewhere in the code I add object to the collection:
...
MyCollection col = new MyCollection()
MyObject obj = new MyObject()
col.Add(obj);

Is it possible now to determine which is obj's parent collection (sth like:
obj.?ParentCollection?) or must I manually track parent collections (e.g.
new MyObject(col) or sth like that). I know object can participate in more
than one collection at the same time (or not) so this may not be possible at
all?

You'd have to have specific support in MyObject for this - an object
doesn't have any inherent idea of whether it's part of another
collection or not.

Jon
 

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