Casting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roshawn

    Casting problem

    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
    InvalidCastExce ption. 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(Cach e("FormItems" ), List(Of Item)) 'error happens here
    Else 'data isn't cached
    Dim ees = From itm In xdoc.Descendant s(nsp + "item") Select New Item With {.Text = _
    itm.@text.ToStr ing, .Value = itm.@value.ToSt ring, .ParentName =
    itm.Parent.@nam e.ToString}
    Cache.Insert("I tems", 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.Collect ions.Generic.Li st`1[_Default+Item]' to type
    'System.Collect ions.Generic.Li st`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.
Working...