Click here to Skip to main content
15,915,065 members
Home / Discussions / C#
   

C#

 
QuestionReplace "inner list" of a generic class inheriting from List<T> ? Pin
BillWoodruff12-Dec-09 2:13
professionalBillWoodruff12-Dec-09 2:13 
AnswerRe: Replace "inner list" of a generic class inheriting from List<T> ? [modified] Pin
PIEBALDconsult12-Dec-09 3:45
mvePIEBALDconsult12-Dec-09 3:45 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
BillWoodruff12-Dec-09 7:07
professionalBillWoodruff12-Dec-09 7:07 
GeneralRe: Replace "inner list" of a generic class inheriting from List<T> ? Pin
PIEBALDconsult12-Dec-09 7:42
mvePIEBALDconsult12-Dec-09 7:42 
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 
Hi Dave,

Thanks for the responsible answer. I am using C#4, by the way.

I'm familiar with using automatic properties, and automtatic property initializers.

What I am curious about here is that :

1. once you've defined an instance of AnyDamnList<sometype> : you can type in the instance name followed by a dot and get all the intellisense you'd expect for an instance of List<sometype> including "non-generic" properties like 'Count. You've got access to methods like 'Add, 'AddRange, GetRange, indexing with [] : in other words your instance "embodies" a List<t>.

2. using your class definition above you still cannot assign another List<int> to an instance of AnyDamnList<int> directly :

// I've renamed your class anyDamnList2
            anyDamnList2<int> aYIntList = new anyDamnList2<int>();

            aYIntList.Add(100);
            aYIntList.Add(200);

            Console.WriteLine(aYIntList[1]);

            List<int> intList3 = new List<int> { 9, 19, 22, 44 };

            aYIntList = intList3;

            Console.WriteLine(aYIntList[3]);


When you get the error at compile time : "Cannot implicitly convert type 'System.Collections.Generic.List<int>' to 'WindowsFormsApplication1.anyDamnList2<int>'. An explicit conversion exists (are you missing a cast?)"

The fact that the error output indicates "an explicit conversion exists."

Leads me to think that somewhere in the "galaxy" of Linq's many operators/extensions that do conversions ( OfType, Cast, ConvertAll, ToList, and so forth ), or just in the typical ways we now do casting, is a way to get this done without, as in your, much appreciated example :

anyDamnList = new AnyDamnList<int>(list);


Which is essentially discarding the current instance (of 'anyDamnList ) and replacing it with a new instance.

A friend of mine has suggested : "look at implementing the IEnumarable<T> interface instead of inheriting from List<T>."

So maybe that's the next step. And, of course, going back to study Skeet's book more Smile | :)

thanks, Bill

"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
PIEBALDconsult12-Dec-09 9:06
mvePIEBALDconsult12-Dec-09 9:06 
GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
Gideon Engelberth12-Dec-09 10:56
Gideon Engelberth12-Dec-09 10:56 
GeneralRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
BillWoodruff13-Dec-09 12:28
professionalBillWoodruff13-Dec-09 12:28 
AnswerRe: Replace "inner list" of a generic class inheriting from List&lt;T&gt; ? Pin
Nicholas Butler12-Dec-09 23:28
sitebuilderNicholas Butler12-Dec-09 23:28 
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 

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.