By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Variables as cookie keys?

P: n/a
Have a look at the following ASP code;

<%Response.Buffer=true%>
<HTML>
<BODY>

<%
Dim itema
Dim itemqa

itema="121"
itemqa=2

Response.Cookies("List")(itema)=itemqa

%>
I want to create a simple shopping cart.
It seems the computer won't let itema be
anything other than a string. How can I
specify an item number without having
to declare it within the script?
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Well by having itema="121" the quotes make it a string.

itemqa=2 without quotes, makes it an integer by default.

So, with all of this said, you need to pick a datatype and stick with
it. If you're trying to get itema = itemqa you could either change
itemqa into a string, or simply make it a string instead of an
integer...

I hope this helps, also I have trouble understanding your last
question. If it's a variable that you are using, I believe you have to
declare it.

Nov 19 '05 #2

P: n/a
Hi Chad,

Thanks for your intervention. However,
a flash of inspiration gave me the answer.

Cookie keys *have* to be strings - they cannot be integers.
I wanted a way of storing integer values and get them back.

<%
Dim itema
Dim sitema
Dim itemqa

itema=121+4
itemqa=2
sitema=CStr(itema)

Response.Cookies("List")(sitema)=itemqa
Response.Cookies("List")("item2")="2"
Response.Cookies("List")("item3")="3"
%>

The answer is a simple conversion from integer (here itema)
to string (sitema) using the CStr command. This allows anyone
to use an integer as a cookie key value. It can be read back by
using the command CInt (if ever the viewer wanted to save
an integer value to a access database for example).
James

"Chad Devine" <su*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Well by having itema="121" the quotes make it a string.

itemqa=2 without quotes, makes it an integer by default.

So, with all of this said, you need to pick a datatype and stick with
it. If you're trying to get itema = itemqa you could either change
itemqa into a string, or simply make it a string instead of an
integer...

I hope this helps, also I have trouble understanding your last
question. If it's a variable that you are using, I believe you have to
declare it.

Nov 19 '05 #3

P: n/a
Ah you're right, thanks for posting the answer. :D

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.