By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,669 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,669 IT Pros & Developers. It's quick & easy.

List(Of clsClass1, clsClass2)?

P: n/a
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
Jul 15 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 15, 9:40*am, "Pieter" <pieterNOSPAMcou...@hotmail.comwrote:
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
Jul 15 '08 #2

P: n/a
On Jul 15, 12:40*pm, "Pieter" <pieterNOSPAMcou...@hotmail.comwrote:
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, T2type 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.
Jul 15 '08 #3

P: n/a
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
Jul 16 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.