Search code examples
.netvb.netcovariancectype

Invalid Types in VB.NET


I have a function which returns a list of products and a page which displays then, but when I try to load the list on the page, I get a cryptic error:

[A]System.Collections.Generic.List`1[Product] cannot be cast to
[B]System.Collections.Generic.List`1[Product].
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

To me, the two types appear identical. The exception is thrown on the line:

Dim products As List(Of Product) = AppDAL.FindProducts(New ProductSearchEventArgs(SearchText, ProductSearchEventArgs.TextCriteria.Contains))

The signature of my method is:

Public Shared Function FindProducts(e As ProductSearchEventArgs) As List(Of Product)

Neither Product, the page which is calling the FindProducts method or the AppDAL class have namespaces, I can't figure out why this doesn't work


Solution

  • While you might not of set a namespace in code, VB always has a default namespace name in the project properties. Have you tried Dim products As List(Of AppDAL.Product) =?