Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > .NET Framework

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-2008, 08:40 AM
Pieter
 
Posts: n/a
List(Of clsClass1, clsClass2)?

Hi,

A little bit linked to my other question: I thought it was possible to have
generic list which accept multiple types of objects. So a statement as ""Dim
MyList as List(Of clsClass1, clsClass2)" would accept instances of both
clsClass1 and clsClass2.

But, it doesn't work :-) Am I simply wrong, or did I do something wrong?

Thanks a lot in advance,


Pieter


Reply With Quote
  #2 (permalink)  
Old 07-15-2008, 08:52 AM
Jon Skeet [C# MVP]
 
Posts: n/a
Re: List(Of clsClass1, clsClass2)?

On Jul 15, 9:40*am, "Pieter" <pieterNOSPAMcou...@hotmail.com> wrote:
> A little bit linked to my other question: I thought it was possible to have
> generic list which accept multiple types of objects. So a statement as ""Dim
> MyList as List(Of clsClass1, clsClass2)" would accept instances of both
> clsClass1 and clsClass2.
>
> But, it doesn't work :-) Am I simply wrong, or did I do something wrong?


No, it doesn't work. What would the effective type of the indexer be,
or the iterator?

Jon
Reply With Quote
  #3 (permalink)  
Old 07-15-2008, 12:30 PM
Pavel Minaev
 
Posts: n/a
Re: List(Of clsClass1, clsClass2)?

On Jul 15, 12:40*pm, "Pieter" <pieterNOSPAMcou...@hotmail.com> wrote:
> Hi,
>
> A little bit linked to my other question: I thought it was possible to have
> generic list which accept multiple types of objects. So a statement as ""Dim
> MyList as List(Of clsClass1, clsClass2)" would accept instances of both
> clsClass1 and clsClass2.
>
> But, it doesn't work :-) Am I simply wrong, or did I do something wrong?


If you want a list of related objects, then it is likely that they
should have a common base class (or implement some common interface).

If you _really_ want a typesafe list of unrelated objects (for
example, because ordering is relevant), then you can use the
Either<T1, T2> type from ECMA TR/89 (http://www.ecma-international.org/
publications/techreports/E-TR-089.htm):

var list = new List<Either<Class1, Class2>>();
list.Add(new Class1());
list.Add(new Class2());
foreach (var item in list)
{
if (item.IsFirst)
{
Class1 c1 = (Class1)item;
...
}
else if (item.IsSecond)
{
Class2 c2 = (Class2)item;
...
}
}

By the way, please do not use "cls" prefix for your classes - this is
against pretty much all naming conventions in the .NET land. Class
names are supposed to begin with a capital letter, and Hungarian
notation is generally frowned upon, but particularly so for type names.
Reply With Quote
  #4 (permalink)  
Old 07-16-2008, 01:49 AM
=?Utf-8?B?U3VydHVyWg==?=
 
Posts: n/a
RE: List(Of clsClass1, clsClass2)?

Cheat and use a List (Of Object) !! ;-)

In your loops then use GetType and CType to process individual members.

(This approach is "wrong" for many reasons, but oh so useful)

--
David Streeter
Synchrotech Software
Sydney Australia


"Pieter" wrote:

> Hi,
>
> A little bit linked to my other question: I thought it was possible to have
> generic list which accept multiple types of objects. So a statement as ""Dim
> MyList as List(Of clsClass1, clsClass2)" would accept instances of both
> clsClass1 and clsClass2.
>
> But, it doesn't work :-) Am I simply wrong, or did I do something wrong?
>
> Thanks a lot in advance,
>
>
> Pieter
>
>
>

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Microsoft > .NET Framework


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 07:54 AM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Loans | Personal Loans | Car Insurance | Problem Mortgage | Loan



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109