Unable to cast object of type...

R

Roshawn

Hello,

I'm having a strange problem as of late. I can't seem to retrieve an object from the
cache. Here a sample of the code I'm using:

'global variable
Dim fItems as Item() 'Item is a custom class

Protected Sub Page_Load
If Not Cache("FormItems") Is Nothing Then
fItems = CType(Cache("FormItems"), Item()) 'error occurs here
Else
'... implementation omitted, but it works
End If
'... rest omitted; only works if there data in the fItems object
End Sub


When I run the above code the first time, all works as expected. I can even access the
cache. However, if I were to make a slight change to any code on the page and rebuild it
(just the page and not the whole site), I can no longer retrieve data from the cache. I
get this error:

System.InvalidCastException: Unable to cast object of type 'Item[]' to type 'Item[]'.

What the heck's going on? I've tried changing the code to use List(Of Item) and
IEnumerable(Of Item), yet the same error shows (the exception message is slightly
different as it mentions List or IEnumerable). Is there something I'm not doing? I'm
using VWD Express 2008.

Thanks,
Roshawn
 
A

Anthony Jones

Roshawn said:
Hello,

I'm having a strange problem as of late. I can't seem to retrieve an object from the
cache. Here a sample of the code I'm using:

'global variable
Dim fItems as Item() 'Item is a custom class

Protected Sub Page_Load
If Not Cache("FormItems") Is Nothing Then
fItems = CType(Cache("FormItems"), Item()) 'error occurs here
Else
'... implementation omitted, but it works
End If
'... rest omitted; only works if there data in the fItems object
End Sub


When I run the above code the first time, all works as expected. I can even access the
cache. However, if I were to make a slight change to any code on the page and rebuild it
(just the page and not the whole site), I can no longer retrieve data from the cache. I
get this error:

System.InvalidCastException: Unable to cast object of type 'Item[]' to type 'Item[]'.

What the heck's going on? I've tried changing the code to use List(Of Item) and
IEnumerable(Of Item), yet the same error shows (the exception message is slightly
different as it mentions List or IEnumerable). Is there something I'm not doing? I'm
using VWD Express 2008.


The problem will be that when you change the page it has to be recompiled as
a new assembly. Any types defined with in that page will no longer be that
same.

When you run the page again you will be trying to cast a type from a
previous incarnation of the page to the type of the current incarnation.
Its this casting which is failing. Since the text name for both types is
the same you get a weird error that says you can't cast object of type x to
type x.

Its not a good idea to place instances of types defined in page in the cache
object. Instead place a .vb file in the App_Code folder and define your
Item type there. That way the item class won't keep changing each time you
change the app. Of course this does lead to it having a wider scope.
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top