strange problem when casting

Can I

Member
Joined
Oct 23, 2006
Messages
12
Location
Coventry, UK
Programming Experience
3-5
hello,

I'm creating an instance of ShoppingCart class when session starts:
VB.NET:
<%@ Import Namespace="Code.business.shopping_cart" %>
...
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
        Session("shoppingCart") = New ShoppingCart()
    End Sub

and using it in my ShoppingCart Web Control
VB.NET:
Imports Code.business.shopping_cart
...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim shoppingCart As Code.business.shopping_cart.ShoppingCart = Session("shoppingCart")
        ...
    End Sub


and that works fine, but when I don't use fully qualified class name, like this:
VB.NET:
Dim shoppingCart As ShoppingCart = Session("shoppingCart")
I'm getting class cast exception:
Unable to cast object of type 'Code.business.shopping_cart.ShoppingCart' to type 'ShoppingCart'.


isn't it odd?
 
Types are assembly specific. If you for example have declared the "same" class in both a webservice and a windows application that consumes it you can't cast a returned webservice instance to the local version, they will per definition be differerent types. One solution is to declare the class in a commonly referenced class library (that way both will use the same assembly qualified type) or make the webservice defined class public and qualify that from the local consumer.
 
Back
Top