473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

List(Of ChildType) Cannot be converted to List(Of BaseType)

My problem is the subject line. Basically, my thinking was that, given
class TestSpec1 inherits from class TestBase, since I can do this...
Dim tst As TestBase = GetTestObj()

Function GetTestObj() As TestSpec1
Return New TestSpec1
End Function

I thought I could do this...

Dim lst As List(Of TestBase) = GetTestList()

Function GetTestList() As List(Of TestSpec1)
Return New List(Of TestSpec1)
End Function

But I can't. Anyway to go about doing this type of setup or am I
barking up the wrong tree?

Thanks as always for any input.
Jan 18 '08 #1
7 1418
Hi ,

There's no generic variance supported in VB or C# at this point in time.
There is variance with arrays which some view as a good thing, some as bad
;)

Let's say you have base class A, and derived types B and C, such that B
inherits A and C inherits A. Now if you declare a list as a List(Of B) and
could cast that to a List(Of A) as such you could then add items of type C
to the List(Of A) which is not really meant to be allowed because the list
is a List(Of B) .

So in a nutshell, that's why it isn't there. The best rule for dealing with
generics is to propagate the generic parameter, e.g:

Function GetTestList(Of T) ()As List(Of T)


"DippyDog" <di*******@gmail.comwrote in message
news:ab**********************************@m34g2000 hsb.googlegroups.com...
My problem is the subject line. Basically, my thinking was that, given
class TestSpec1 inherits from class TestBase, since I can do this...
Dim tst As TestBase = GetTestObj()

Function GetTestObj() As TestSpec1
Return New TestSpec1
End Function

I thought I could do this...

Dim lst As List(Of TestBase) = GetTestList()

Function GetTestList() As List(Of TestSpec1)
Return New List(Of TestSpec1)
End Function

But I can't. Anyway to go about doing this type of setup or am I
barking up the wrong tree?

Thanks as always for any input.
Jan 18 '08 #2
Hello Bill,
Let's say you have base class A, and derived types B and C, such that
B inherits A and C inherits A.
Ok
Now if you declare a list as a List(Of B) and could cast that to a List(Of
A) as such you could then add
items of type C to the List(Of A) which is not really meant to be allowed
because the list is a List(Of B).

I'm not sure if your example is what was required by the OP. (Perhaps a mistype?)

I would like to be able to ...
....cast a List(Of B) to a List(Of A) (Since all B's contain data nessecary
to qualifiy as an A)
....cast a List(Of C) to a List(Of A) (Since all C's contain data nessecary
to qualifiy as an A)

But would understand not being able to ...
....cast List(Of A) to List(Of B)
....cast List(Of A) to List(Of C)
....cast List(Of C) to List(Of B)
....cast List(Of B) to List(Of C)

While I appreciate that this is not currently allowed, Is this percieved
as something that will arrive in some hypothetical vlanguage variation down
the line, or is there a technical reason why we shouldn't do this?

--
Rory

Jan 18 '08 #3
"DippyDog" <di*******@gmail.comschrieb
My problem is the subject line. Basically, my thinking was that,
given class TestSpec1 inherits from class TestBase, since I can do
this...
Dim tst As TestBase = GetTestObj()

Function GetTestObj() As TestSpec1
Return New TestSpec1
End Function

I thought I could do this...

Dim lst As List(Of TestBase) = GetTestList()

Function GetTestList() As List(Of TestSpec1)
Return New List(Of TestSpec1)
End Function

But I can't. Anyway to go about doing this type of setup or am I
barking up the wrong tree?

Thanks as always for any input.
The two types of the items in the list are derived from each other
whereas the lists themselves are not derived from each other.

In other words,
TestSpec1 is derived from TestBase =works
List(Of TestSpec1) is not derived from List(Of TestBase) =doesn't work
Armin

Jan 18 '08 #4
Hi Rory,

<inline>

"Rory Becker" <ro********@newsgroup.nospamwrote in message
news:20*************************@news.microsoft.co m...
Hello Bill,
>Let's say you have base class A, and derived types B and C, such that
B inherits A and C inherits A.

Ok
>Now if you declare a list as a List(Of B) and could cast that to a
List(Of
A) as such you could then add
>items of type C to the List(Of A) which is not really meant to be allowed
because the list is a List(Of B).

I'm not sure if your example is what was required by the OP. (Perhaps a
mistype?)
Nope, not a mistype.

I would like to be able to ...
...cast a List(Of B) to a List(Of A) (Since all B's contain data
nessecary to qualifiy as an A)
...cast a List(Of C) to a List(Of A) (Since all C's contain data
nessecary to qualifiy as an A)

If you have a List(Of A), you can insert or add items to it that are of type
B or type C, because B and C both derive from A. So soon as you cast a
list(Of B) to a list(Of A) you have removed the type safety, allowing
objects of type C to be added to the list.

Now with a List(Of...) class, this issue isn't that big because the backing
store is an array, and because arrays do allow variance, they have to do
type checking at runtime, so an exception will be thrown. But if you were
to write your own collection class and used a different storage mechanism,
the exception might not occur on add, it might only occur later on read
when, inside your collection class, you try to cast to the desired type.

So variance comes at a cost, and it's for that reason we don't have generic
variance at present.

But would understand not being able to ...
...cast List(Of A) to List(Of B)
...cast List(Of A) to List(Of C)
...cast List(Of C) to List(Of B)
...cast List(Of B) to List(Of C)

While I appreciate that this is not currently allowed, Is this percieved
as something that will arrive in some hypothetical vlanguage variation
down the line, or is there a technical reason why we shouldn't do this?

See above as to the costs. As to will languages go in this direction, yes
I believe so. VB today in 2008 now allows certain types do delegate
variance it didn't allow before. And the CLR itself allows for variance.
The question is how do we surface that in a type safe way that in itself
doesn't become more onerous than the problem we are seeking to solve ;)
Some of the C# team have been blogging about variance in C#.Next (4). If
you look at the complexities that arise from it, it raises the question is
it just simpler to propagate the generic variable instead of requiring the
variance ? For example, given Item_Get is safe, yet Item_Set is not in the
case of casting List(of B) to List(of A), you could imagine defining an
interface that specifies only the Item_Get part and using that, perhaps as a
dynamic interface. In any case, today those thoughts are still "out there",
and in code today in practice the best option is to either use non generic
interfaces or preferably, propagate the generic parameters throughout.




Jan 18 '08 #5
Hello Bill,
Now if you declare a list as a List(Of B) and could cast that to a
List(Of A) as such you could then add items of type C to the List(Of A)
which is not really meant to be
allowed because the list is a List(Of B).
ok... yeah... sure :)

I got it now.

Totally agree.

I really must stop replying to posts before my caffine level has reached
the correct level :)

--
Rory
Jan 18 '08 #6
Excellent postings, y'all! Thanks a heap. Special thanks to Bill. It
took me a few passes to get what you were saying in your first post,
but it was exactly what I needed. In particular was your suggestion
about passing the list type along. I think my coworker and I are going
with a function returning List(Of T As IInterface), using the
interface to keep type safety, if this works.
Thanks again!
Jan 18 '08 #7
Thanks . Glad it helped :)

"DippyDog" <di*******@gmail.comwrote in message
news:c5**********************************@m34g2000 hsb.googlegroups.com...
Excellent postings, y'all! Thanks a heap. Special thanks to Bill. It
took me a few passes to get what you were saying in your first post,
but it was exactly what I needed. In particular was your suggestion
about passing the list type along. I think my coworker and I are going
with a function returning List(Of T As IInterface), using the
interface to keep type safety, if this works.
Thanks again!
Jan 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: foggy | last post by:
Derived1 and Derived2 are derived from BaseClass: list<BaseClass&> lst; Derived1 d1; Derived2 d2; lst.push_back(d1); lst.push_back(d2); Can a list be created like above so that d1, d2......
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
5
by: DevarajA | last post by:
I'd like to create a linked list or a simple array of pointers to function, but I can't find out how. I'm trying to do it like this: struct fn_pointer{ void *pt_; /*the address*/ char...
6
by: JezB | last post by:
Can I programatically get a list of Pages that are part of an ASP.NET assembly ?
8
by: Matthew Wilson | last post by:
I want to verify that three parameters can all be converted into integers, but I don't want to modify the parameters themselves. This seems to work: def f(a, b, c): a, b, c = ...
6
by: python101 | last post by:
I have a list of integers s= to be converted to s= I tried s= for i in s: str(a) '2'
8
by: metaperl.com | last post by:
Pyparsing has a really nice feature that I want in PLY. I want to specify a list of strings and have them converted to a regular expression. A Perl module which does an aggressively optimizing...
1
by: nuffnough | last post by:
I have defined two classes with one common field (called code) and several different fields. In class A there is only one instance of any given code as all items are individual. In class B, there...
10
by: Ali Shirvani | last post by:
Hi all, I want to get list of all functions that are defined in the current C code, for example if I have the code below: int f(int c, int d){ int a = 10; int b = 12; return c+a; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.