Casting problem

R

Roshawn

Hi,

I have a generic list of objects which I place in the cache. When trying to retrieve the
data in the cache and place it in another object of the same type, I get an
InvalidCastException. Here's my code:


'global variable
Dim fItems as List (Of Item)'Item is a custom object

'inside the Page_Load event
'other code omitted
If Not Cache("Items") Is Nothing Then
fItems = DirectCast(Cache("FormItems"), List(Of Item)) 'error happens here
Else 'data isn't cached
Dim ees = From itm In xdoc.Descendants(nsp + "item") Select New Item With {.Text = _
(e-mail address removed), .Value = (e-mail address removed), .ParentName =
(e-mail address removed)}
Cache.Insert("Items", ees.ToList)
fItems = ees.ToList
End If



Now, when I first run the code, everything goes as expected. But when I try to access the
cached data, I get this error:


"Unable to cast object of type 'System.Collections.Generic.List`1[_Default+Item]' to type
'System.Collections.Generic.List`1[_Default+Item]'"


Instead of using DirectCast I've even tried using CType but still I get the same problem.
Could somebody help me? I have no idea what I'm doing wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top