>>>>Hi Viv, I worked around using different classes when building my list by setting some properties on the class along the lines of
>>>>
>>>>
>>>>
>>>>if(this.listtype == "Listb")
>>>> this.Data.Add(new Class1(){field values go here});
>>>>else
>>>> this.Data.Add(new Class2(){field values go here});
>>>>
>>>>etc, etc
>>>>
>>>>
>>>>
>>>>The Data member of my class is defined as
>>>>
>>>>
>>>>List<MyBaseClassThatAllClassesAreBasedOn> Data ;
>>>>
>>>>
>>>>All appears to work except the values of my list object properties are either null or empty defaults ( the objects themselves are not null ). I've traced the code in the debugger and the classes are definitely populated with correct values but once they are added to the list they are null ? very weird.
>>>
>>>I think you have a Catch22 there. If you define the same properties in the base class they will be null ; if you don't then they won't be visible in the collection unless you cast to the derived class (i.e : ((Class2)collection[0]).Something) - which sort of defeats the object. Common interface should be the way to go.
>>>
>>>How do your Class1 and Class2 differ ?
>>
>>Think you have to
>>(1) Define the list in terms of the interface
>>
>>List<IMyInterface> Data ;
>>
>>
>>(2) Cast to the interface before adding them to the list
>>
>>
>>if(this.listtype == "Listb")
>> this.Data.Add( (IMyInterface) new Class1(){field values go here});
>>else
>> this.Data.Add( (IMyInterface)new Class2(){field values go here});
>>
>>
That would work but Pete seemed to have moved away from an interface approach to derived classes. Thinking about it using delegates might have been a better way from the start ?
Dunno -
I would use a delegate if there is only one method involved ( or perhaps two )
If you need more methods, then an interface seems more appropiate. And you can add easily methods to an interface while developping
Also, you can use a static method for a delegate whereas you need an instance for an interface
The problem was that Pete already had made the analysis, we didn't ( at least I didn't) see the overall picture
Gregory