If you're trying to avoid harcoding the cast you can store get the 'Class'
object for the class name which you can
store as well (as part of the key maybe!)

Cheers,


Tammam Al-Hamwy



Michael Nash <[EMAIL PROTECTED]> on 04/20/99 11:32:30 AM

Please respond to "A mailing list for discussion about Sun Microsystem's
      Java              Servlet API Technology."
      <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Tom Al-Hamwy/Toronto/IBM)
Subject:  Re: [Fwd: session getValue question]





Jere:

You must do an explicit cast, as yout error suggests, because the getValue
is returning a "generic" Object - it doesn't know it's a cartitem.

If you say:
        B = (cartitem) session.getValue(A.getName());

you should be OK I think.

And I think you might be looking for something like an "equals" method
rather than an overloaded operator, depending on what you're doing. (I'm
doing a shopping cart as well, among other stuff, BTW)

Mike Nash

-----Original Message-----
From: Jere Robinson [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 20, 1999 6:30 AM
To: [EMAIL PROTECTED]
Subject: [Fwd: session getValue question]


Jere Robinson wrote:
>
> Hey guys, little 'ol me has been put on as head of my ISP's Java Servlet
> shopping cart project.  I have a question about the HttpSession method
> getValue.  It is supposed to return the object to which the String value
> parameter (name) points, but when I try the following:
>
>     cartitem A = new cartitem();
>     A.putName("Bar of Soap");
>     A.putPrice(150);
>     A.putQuantity(1);
>
>     session.putValue(A.getName(),A);
>     cartitem B = new cartitem();
>     B = session.getValue(A.getName());
>
> It chokes on ' B = session.getValue(A.getName()); ' at compile time,
> telling me I need to cast it:
>
> CartTest.java:31: Incompatible type for =. Explicit cast needed to
> convert java.lang.Object to cartitem.
>     B = session.getValue(A.getName());
>       ^
> What do I need to get this to work?
> Oh, I need to overload the = operator in my cartitem class, don't I?
> I'll send this in case I'm going down the wrong path.  Even if I'm
> right, how do we overload operators in Java?
>
> Thanks in advance!
> Jere

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to