Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Generic list question
Message
From
20/11/2014 12:35:57
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01611163
Message ID:
01611251
Views:
40
>>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});
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform