Visual C# Developer Center > Visual C# Forums > Visual C# Language > binding list of objects where some objects inherit others.
Ask a questionAsk a question
 

Answerbinding list of objects where some objects inherit others.

  • Wednesday, November 04, 2009 1:48 PMDavid Shaw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a binding list of objects where the objects are in a heirarchy, ie base class A, B inherits A, C inherits A, D inherits C.
    I want to be able to select an object in a list box, where the Name property of the objects are listed, and when a particular object is selected, say C, read a property of C that does not exist on A or B. My code looks like this:

    foreach

     

    (Classification c in f.Classifications)

    {

     

    string name = c.Name;

     

    if (c is ClassificationWithArgument)

    {

     

    if (name == "CALL DIALOG")

    {

    subDialogNumber = c.Argument;

    }

    }

    }

    Here, Classification is the base class, and ClassificationWithArgument is one of the derived classes which has a property called Argument which does not exist in the base class.
    I am trying to search all objects in the list until I find one with a name matching the name selected in the list box, this has to be of type ClassificationWithArgument (enforced by the selection criteria), then read the Argument property of the object.
    The code will not compile - I get an error that Classification does not contain a definition for Argument and no extension.......etc.

    Is there a way I can iterate through my list and when I find a particular object read a property not common to all items in the list?


    dshaw

Answers

  • Thursday, November 05, 2009 5:56 AMGuang-Ming Bian - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi dshaw,

    Since object c is defined by Classification class, although object c is ClassificationWithArgument type, we still can't access property in ClassificationWithArgument  class. We have to convert it to ClassificationWithArgument explicitly. Like the code below:

    if (c is ClassificationWithArgument)  
    {
    ClassificationWithArgument temp;

    temp=(ClassificationWithArgument)c;

    if
    (name == "CALL DIALOG")
    {
    subDialogNumber = temp.Argument;
    }
    }

     

    If you have any question, please let me know.


    Have a nice day.


    Best regards,

    Guang-Ming Bian - MSFT

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help

All Replies

  • Thursday, November 05, 2009 5:56 AMGuang-Ming Bian - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi dshaw,

    Since object c is defined by Classification class, although object c is ClassificationWithArgument type, we still can't access property in ClassificationWithArgument  class. We have to convert it to ClassificationWithArgument explicitly. Like the code below:

    if (c is ClassificationWithArgument)  
    {
    ClassificationWithArgument temp;

    temp=(ClassificationWithArgument)c;

    if
    (name == "CALL DIALOG")
    {
    subDialogNumber = temp.Argument;
    }
    }

     

    If you have any question, please let me know.


    Have a nice day.


    Best regards,

    Guang-Ming Bian - MSFT

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help

  • Thursday, November 05, 2009 9:43 AMDavid Shaw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Guang
    That works perfectly.

    Your help is much appreciated.

    Regards

    Dave
    dshaw