Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generic list question
Message
De
21/11/2014 02:00:03
 
 
À
20/11/2014 12:35:57
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01611163
Message ID:
01611268
Vues:
30
>>>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 ?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform