Click here to Skip to main content
15,885,847 members
Home / Discussions / C#
   

C#

 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
BillWoodruff12-Dec-09 8:09
professionalBillWoodruff12-Dec-09 8:09 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
PIEBALDconsult12-Dec-09 9:49
mvePIEBALDconsult12-Dec-09 9:49 
AnswerRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
DaveyM6912-Dec-09 7:44
professionalDaveyM6912-Dec-09 7:44 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
BillWoodruff12-Dec-09 8:30
professionalBillWoodruff12-Dec-09 8:30 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
PIEBALDconsult12-Dec-09 9:06
mvePIEBALDconsult12-Dec-09 9:06 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
Gideon Engelberth12-Dec-09 10:56
Gideon Engelberth12-Dec-09 10:56 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
BillWoodruff13-Dec-09 12:28
professionalBillWoodruff13-Dec-09 12:28 
AnswerRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
Nicholas Butler12-Dec-09 23:28
sitebuilderNicholas Butler12-Dec-09 23:28 
BillWoodruff wrote:
Here's the question : is there any other way than using 'Clear() and then 'AddRange
to replace the "virtual inner list" <t> "embodied" via inheritance in the class ?


No. anyDamnList "is a" List<int> -> the particular List is an inherent part of a particular instance of anyDamnList -> you cannot replace just the "List part" of an anyDamnList.

That's just the way inheritance works.

LINQ won't help either. LINQ mostly knows about interfaces: IEnumerable and IQueryable; and a little about List and Array. It knows nothing about anyDamnList.

If you want to be able to plug in a new List part into an anyDamnList, you would have to use composition:

C#
public class anyDamnList
{
  private List<int> _InnerList;

  public void ReplaceInnerList( List<int> newList )
  {
    _InnerList = newList;
  }
}


If you also want anyDamnList to behave like a List<int>, so you can do this:

C#
int count = myAnyDamnList.Count;


Then you can implement the same interfaces as List<int>:

C#
public class anyDamnList : IList<int>


Unfortunately, you have to implement the interfaces yourself:

C#
int Count { get { return _InnerList.Count; } }


This is a pain, but is do-able. Since C# doesn't support multiple inheritance, which means you end up using composition a lot, it would be nice IMHO if there was an implementedBy keyword:

C#
public class anyDamnList : IList<int>
{
  IList<int> implementedBy _InnerList;
}


But I've never heard anyone else even suggest this.

Nick

----------------------------------
Be excellent to each other Smile | :)

GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
BillWoodruff13-Dec-09 12:21
professionalBillWoodruff13-Dec-09 12:21 
GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
Nicholas Butler13-Dec-09 22:45
sitebuilderNicholas Butler13-Dec-09 22:45 
GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? [modified] Pin
BillWoodruff14-Dec-09 4:50
professionalBillWoodruff14-Dec-09 4:50 
Answera "partial solution" to : re : Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
BillWoodruff13-Dec-09 12:51
professionalBillWoodruff13-Dec-09 12:51 
QuestionUsing Methods within a Switch Case Pin
DevonDaDude11-Dec-09 22:47
DevonDaDude11-Dec-09 22:47 
AnswerRe: Using Methods within a Switch Case Pin
Abhinav S11-Dec-09 22:56
Abhinav S11-Dec-09 22:56 
GeneralRe: Using Methods within a Switch Case Pin
OriginalGriff11-Dec-09 23:03
mveOriginalGriff11-Dec-09 23:03 
GeneralRe: Using Methods within a Switch Case Pin
Abhinav S11-Dec-09 23:09
Abhinav S11-Dec-09 23:09 
GeneralRe: Using Methods within a Switch Case Pin
Saksida Bojan12-Dec-09 1:49
Saksida Bojan12-Dec-09 1:49 
GeneralRe: Using Methods within a Switch Case Pin
Richard MacCutchan12-Dec-09 3:08
mveRichard MacCutchan12-Dec-09 3:08 
GeneralRe: Using Methods within a Switch Case Pin
DevonDaDude15-Dec-09 18:20
DevonDaDude15-Dec-09 18:20 
AnswerRe: Using Methods within a Switch Case Pin
OriginalGriff11-Dec-09 23:01
mveOriginalGriff11-Dec-09 23:01 
AnswerRe: Using Methods within a Switch Case Pin
PIEBALDconsult12-Dec-09 3:59
mvePIEBALDconsult12-Dec-09 3:59 
GeneralRe: Using Methods within a Switch Case Pin
DevonDaDude12-Dec-09 15:45
DevonDaDude12-Dec-09 15:45 
QuestionUse of &lt;&lt; and &gt;&gt; [modified] Pin
Joe Rozario11-Dec-09 19:16
Joe Rozario11-Dec-09 19:16 
AnswerRe: Use of &lt;&lt; and &gt;&gt; Pin
Abhinav S11-Dec-09 19:56
Abhinav S11-Dec-09 19:56 
AnswerRe: Use of &lt;&lt; and &gt;&gt; [modified] Pin
DaveyM6911-Dec-09 21:27
professionalDaveyM6911-Dec-09 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.