For the problem I have these classes:
Public MustInherit Class BaseLeaf(Of T)
Implements IBaseLeaf
// etc
Public Class WebsiteLeaf
Inherits BaseLeaf(Of Headline)
// etc
Public Class WebsiteCollection
Inherits BaseCollection(Of WebsiteLeaf)
// etc
Public Class SubscriptionList
Private mCollection As BaseCollection(Of IBaseLeaf)
Public Sub LoadSubscriptions(ByVal collection As BaseCollection(Of IBaseLeaf))
mCollection = collection
End Sub
In the Main class I am trying to call the following function:
Private Sub FetchSubscriptions(ByVal websites As WebsiteCollection)
gUser.SubscriptionList.LoadSubscriptions(websites)
// code
End Sub
This will access LoadSubscriptions with passing the “websites” variable.
As you can see LoadSubscriptions expects a BaseCollection(Of IBaseLeaf).
The “websites” variable is a WebsiteCollection, which is a BaseCollection(Of WebsiteLeaf(Of Headline)).
Now I am getting the error: Value of type ‘WebsiteCollection’ cannot be converted to ‘BaseCollection(Of IBaseLeaf)’.
What I am doing wrong here?
If class
Bis a descendent of classAthis does not mean thatCollection(Of B)is a descendant ofCollection(Of A)!If you have these definitions
and you could call the method like this
then the method would try to add a
Dateto the list which is actually a list of strings! Since the parameter is typed asobjectthe date would be boxed (i.e. converted to an object) but not converted to string.Therefore the collections
List(Of X)andList(Of Y)are never compatible, even ifYinheritsX.Let’s look at another example
What happens if you call
LoadSubscriptionswith aWebsiteCollection?