Click here to Skip to main content
15,881,752 members
Home / Discussions / C#
   

C#

 
GeneralRe: BSCS FINAL PROJECT Pin
Pete O'Hanlon6-Sep-10 2:41
mvePete O'Hanlon6-Sep-10 2:41 
GeneralRe: BSCS FINAL PROJECT Pin
DaveyM696-Sep-10 3:05
professionalDaveyM696-Sep-10 3:05 
GeneralRe: BSCS FINAL PROJECT Pin
Pete O'Hanlon6-Sep-10 3:15
mvePete O'Hanlon6-Sep-10 3:15 
GeneralRe: BSCS FINAL PROJECT Pin
DaveyM696-Sep-10 4:19
professionalDaveyM696-Sep-10 4:19 
GeneralRe: BSCS FINAL PROJECT Pin
Mycroft Holmes6-Sep-10 22:51
professionalMycroft Holmes6-Sep-10 22:51 
QuestionCalendarExtender popup appearing when Enter key is pressed. Pin
dev_asp_shreshtha6-Sep-10 0:48
dev_asp_shreshtha6-Sep-10 0:48 
AnswerRe: CalendarExtender popup appearing when Enter key is pressed. Pin
dan!sh 6-Sep-10 0:59
professional dan!sh 6-Sep-10 0:59 
QuestionNeed help with co/contravariance and generic lists :s Pin
Lee Reid5-Sep-10 23:12
Lee Reid5-Sep-10 23:12 
Hi guys,
I need to find a way around a problem so that I can have an array containing lists of different types, where those types are all derived from the same class. Unfortunately I'm at the end of my knowledge of covariance etc so any help would be greatly appreciated.

To explain better:
I have a class ('class X') which has several lists in it. The lists contain different types, but these types all derive from the same abstract class. I need to be able to iterate through all items (i.e. across and within the lists) as though all the lists were one. For various reasons, the lists must remain as separate objects. Furthermore, 'Class X' is inherited, and derived classes have additional lists...

My solution was to create an array containing these lists (which makes iterating with an unknown number of lists very easy), but I hit problems very quickly (e.g. see code below). Can anyone show me how to do something like this, or find a more elegant way to do it?

Here's a much smaller, simple example of the kind of thing I'm trying to do:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    abstract class A
    {    }

    class B : A
    {    }

    class C : A
    {    }


    class ClassX
    {
        static void DoStuff()
        {
            List<B> listB = new List<B>();
            List<C> listC = new List<C>();

            List<A>[] arrayOfLists = new List<A>[2];

            arrayOfLists[0] = listB;//Error	1	Cannot implicitly convert type 'System.Collections.Generic.List<ConsoleApplication1.B>'
            arrayOfLists[1] = listC;//Error	2	Cannot implicitly convert type 'System.Collections.Generic.List<ConsoleApplication1.C>'

            arrayOfLists[0] = listB.Cast<A>().ToList();//Subsequent changes in list B are not seen an arrayoflists because a new object is created

            List<List<A>> listOfLists = new List<List<A>>();
            listOfLists.Add(listB);//Error	4	Argument 1: cannot convert from 'System.Collections.Generic.List<ConsoleApplication1.B>' to 'System.Collections.Generic.List<ConsoleApplication1.A>'
        }
    }
}


Thanks!
Lee
AnswerRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz5-Sep-10 23:50
Kubajzz5-Sep-10 23:50 
GeneralRe: Need help with co/contravariance and generic lists :s [modified] Pin
Lee Reid6-Sep-10 0:41
Lee Reid6-Sep-10 0:41 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz6-Sep-10 1:06
Kubajzz6-Sep-10 1:06 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 2:02
Lee Reid6-Sep-10 2:02 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Kubajzz6-Sep-10 2:13
Kubajzz6-Sep-10 2:13 
AnswerRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 0:41
professionalDaveyM696-Sep-10 0:41 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 1:12
Lee Reid6-Sep-10 1:12 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 1:31
professionalDaveyM696-Sep-10 1:31 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 1:54
professionalDaveyM696-Sep-10 1:54 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Lee Reid6-Sep-10 2:08
Lee Reid6-Sep-10 2:08 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
DaveyM696-Sep-10 2:13
professionalDaveyM696-Sep-10 2:13 
GeneralRe: Need help with co/contravariance and generic lists :s Pin
Pete O'Hanlon6-Sep-10 3:41
mvePete O'Hanlon6-Sep-10 3:41 
AnswerRe: Need help with co/contravariance and generic lists :s Pin
PIEBALDconsult6-Sep-10 7:32
mvePIEBALDconsult6-Sep-10 7:32 
QuestionHow to deploy an Outlook 2003 add-in application(C# and VS 2008)? Pin
milestanley5-Sep-10 22:57
milestanley5-Sep-10 22:57 
QuestionCould use some help please. Pin
pestman5-Sep-10 22:01
pestman5-Sep-10 22:01 
AnswerRe: Could use some help please. Pin
Kubajzz5-Sep-10 22:21
Kubajzz5-Sep-10 22:21 
GeneralRe: Could use some help please. Pin
pestman6-Sep-10 6:27
pestman6-Sep-10 6: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.