.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > Referencing a Collections Item's Parent Class (Containing Class)
Ask a questionAsk a question
 

AnswerReferencing a Collections Item's Parent Class (Containing Class)

  • Friday, November 06, 2009 10:25 PMConstansere Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I need to retrieve a property value from the parent class of a collection item. how do i reference the class at runtime?

Answers

  • Friday, November 06, 2009 10:36 PMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is no way to do this, unless you've saved the parent inside of the class.

    This is because there really can't be a way to do it.  Say you have nothing but an instance of "SomeClass" -

        SomeClass theClassInstance = classList[3];

    There'd be no way to get "classList" from that, since it could be:

    1) Not stored in a collection
    2) Stored in a collection multiple times
    3) Stored in multiple collections

    If, however, you need this for your particular scenario, what you could do is have a custom collection implementation, and save a reference to it in each member.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer byConstansere Saturday, November 07, 2009 5:57 PM
    •  

All Replies

  • Friday, November 06, 2009 10:36 PMReed Copsey, Jr. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There is no way to do this, unless you've saved the parent inside of the class.

    This is because there really can't be a way to do it.  Say you have nothing but an instance of "SomeClass" -

        SomeClass theClassInstance = classList[3];

    There'd be no way to get "classList" from that, since it could be:

    1) Not stored in a collection
    2) Stored in a collection multiple times
    3) Stored in multiple collections

    If, however, you need this for your particular scenario, what you could do is have a custom collection implementation, and save a reference to it in each member.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked As Answer byConstansere Saturday, November 07, 2009 5:57 PM
    •