PC Review


Reply
Thread Tools Rate Thread

List(Of clsClass1, clsClass2)?

 
 
Pieter
Guest
Posts: n/a
 
      15th Jul 2008
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
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Jul 2008
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
 
Pavel Minaev
Guest
Posts: n/a
 
      15th Jul 2008
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
 
SurturZ
Guest
Posts: n/a
 
      16th Jul 2008
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

Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use VBA to reset data validation (=list) value to first value in that list (list is a named range) ker_01 Microsoft Excel Programming 7 27th Oct 2008 04:13 PM
Compare List A to List B, Return List B Items Not in List A zwestbrook Microsoft Excel Programming 4 18th Sep 2008 11:32 PM
List(Of clsClass1, clsClass2)? Pieter Microsoft C# .NET 3 16th Jul 2008 03:49 AM
List(Of clsClass1, clsClass2)? Pieter Microsoft VB .NET 3 16th Jul 2008 03:49 AM
list 1 has 400 names List 2 has 4000. find manes from list 1 on 2 =?Utf-8?B?RWQ=?= Microsoft Excel Worksheet Functions 5 12th Sep 2005 10:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:38 PM.