binding list of objects where some objects inherit others.
- 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
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
- Marked As Answer byDavid Shaw Thursday, November 05, 2009 9:41 AM
- Edited byGuang-Ming Bian - MSFTMSFT, ModeratorThursday, November 05, 2009 10:16 AM
All Replies
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
- Marked As Answer byDavid Shaw Thursday, November 05, 2009 9:41 AM
- Edited byGuang-Ming Bian - MSFTMSFT, ModeratorThursday, November 05, 2009 10:16 AM
- Thanks Guang
That works perfectly.
Your help is much appreciated.
Regards
Dave
dshaw


