Has someone a hint what I’m doing wrong in VB.Net?
Module Module1
Interface ISearch(Of T As ISearchResult)
Function ids() As List(Of T)
End Interface
Interface ISearchResult
Function id() As String
End Interface
MustInherit Class BasicSearch(Of T As ISearchResult)
Implements ISearch(Of T)
MustOverride Function ids() As System.Collections.Generic.List(Of T) Implements ISearch(Of T).ids
End Class
Class Search
Inherits BasicSearch(Of SearchResult)
Implements ISearch(Of SearchResult)
Overrides Function ids() As System.Collections.Generic.List(Of SearchResult)
Return New List(Of SearchResult)
End Function
End Class
Class SearchResult
Implements ISearchResult
Public Function id() As String Implements ISearchResult.id
Return "id"
End Function
End Class
Sub Main()
Dim foo As New Search()
Dim bar As Object = foo
Dim foobar As ISearch(Of ISearchResult) = foo
End Sub
End Module
The third cast isn’t working.
Why?
did I miss a oop lesson?
thanks
An
ISearch(Of SearchResult)isn’t anISearch(Of ISearchResult)– they have different generic type parameters.Searchis anISearch(Of SearchResult).Brian’s answer covers the covariance, etc, stuff for .NET 4 that I’d planned to add to this question later (I wrote the initial answer quickly and then had to go offline – by the time I got back, Brian had answered)
To answer mr. moes comment – Imagine if
ISearchhad another method:and assuming we then implement that in
Search(which, remember, isISearch(Of SearchResult), soTisSearchResult). And assume we had something else that implementsISearchResult, say:Now, if your cast worked, we could now call:
But this can’t work – the implementation of
AddIDthat we’re calling is the one implemented bySearch– and that function is only expecting to receive objects of typeSearchResult.