Multiple ReadOnlyBase objects in ReadOnlyListBase?

Multiple ReadOnlyBase objects in ReadOnlyListBase?

Old forum URL: forums.lhotka.net/forums/t/6663.aspx


reagan123 posted on Tuesday, March 24, 2009

Hello,
Quick question.  Let's say I have two ReadOnlyBase objects Object1 and Object2.  Each of these objects have their own class and different properties.  Is it possible to get a ReadOnlyListBase object that will populate with both types of objects at the same time?

Thanks!


RockfordLhotka replied on Tuesday, March 24, 2009

Yes, you just need to define some non-generic (polymorphic) commonality between Object1 and Object2. The easiest thing is to use an interface like IObject1and2:

public class MyList : ReadOnlyListBase<MyList, IObject1and2>
{
}

 

reagan123 replied on Tuesday, March 24, 2009

I'll give it a shot... i'm still new to some of these concepts.

Thanks Rocky for your help :)

reagan123 replied on Tuesday, March 24, 2009

Just to clarify....i'm assuming you mean that the interface would provide certain properties that would be required in each object.  My list then could be data bound to a grid (or whatever) and display those fields the two have in common... Is this correct?

RockfordLhotka replied on Tuesday, March 24, 2009

Well, kind of. The UI stuff is tricky, because data binding won’t use interfaces.

 

So yes, the interface will define what is common in each object, but data binding will actually use reflection to interact with the objects – which will work as long as both types have a common set of public properties for data binding to use – otherwise you’ll get a runtime failure.

 

Rocky

Copyright (c) Marimer LLC