I’m trying to use a variable that is simply a list of a list of strings.
I’ve declared it as follows:
Dim iRows As New List(Of List(Of String))
Then I’m trying to pass it as a parameter to another method and I’ve defined the method as follows:
Public Sub Import(ByVal Rows As IList(Of IList(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)
Next
End Sub
Unfortunately, when I try to run that code I get the following error where it tries to pass my variable to my method.
System.InvalidCastException was unhandled by user code
Message=”Unable to cast object of type ‘System.Collections.Generic.List1[System.Collections.Generic.List1[System.String]]’ to type ‘System.Collections.Generic.IList1[System.Collections.Generic.IList1[System.String]]’.”
When I change the method definition to use the types rather than the interfaces as follows, it works.
Public Sub Import(ByVal Rows As List(Of List(Of String)))
For Each Row As IList(Of String) In Rows
ImportRow(Row)
Next
End Sub
So, is it not possible to use generics with interfaces in this way? It works fine as long as I’m not nesting them.
Yes, it is possible to create an
IList(Of IList(Of String))– but aList(Of (List(Of String))isn’t one. Consider that in the latter case you could callas a string array implements
IList(Of String).Basically this is exactly the same as considering an
IList(Of Fruit)and aList(Of Banana)– a bunch of bananas isn’t a fruit-bowl, because you can’t add an apple to it.In your case, you’d need to create a
List(Of IList(Of String))instead – or declare anIList(Of List(Of String)).Now interestingly, you could use a
List(Of List(Of String))as anIEnumerable(Of IEnumerable(Of String))in .NET 4 due to generic covariance and contravariance –IEnumerable(Of T)is covariant inT. However,IList(Of T)is invariant.Generic covariance and contravariance is a tricky topic. Eric Lippert has written a great deal about it – using C# as the language rather than VB, but hopefully you can still understand it.