Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array help

Status
Not open for further replies.

ecannizzo

Programmer
Sep 19, 2000
213
US
I am displaying questions one by one on a web page. When the user gets the question wrong I want to store the questions id in an array. I'm having a problem keeping the id stored. The array seems to know there is supposed to be an element there, but I only ever get the last id. Here is my code.

if not(blnGotRight) then
redim preserve ids(session("NoInArray")) 'put how many are in array
ids(ubound(ids)) = (request("ID"))
end if

for each id in ids
Response.Write "*" & id & "*, "
next

Here is the answer I get:
**, **, **, *63*,

Can anyone help me please?

Thanks!
 
The reason you only get one answer, the last answer is this: when you redim an array, you lose all info stored in the array. So all those that were previously in the array are gone. Now with the newly dimensioned array, the latest one is added in. You need to preserve when you redim...ex.
redim preserve arrayName(arraySize)

The old stuff will stay in...you can add new stuff. If the new array size is < the old array size, those elements/values at the end will not exist in the new array because the array loses those from resizing.

Here is an example of how it all works:

**********************************************************
'original, has to be blank. can't redim something that has a set size at the outset.
dim a()

'set a size
redim a(2)

'set values
a(0)=&quot;a&quot;
a(1)=&quot;b&quot;
a(2)=&quot;c&quot;

dim elem

for each elem in a
msgbox elem
next

'size is one less now
redim preserve a(1)

for each elem in a
msgbox elem
next
**********************************************************

Hope this makes sense.

Mike
 
Actually, I did preserve it. Am I preserving it wrong?
 
Ok, I didn't see the preserve in there. Do this. For every variable you get, do a response.write of it to the page. This will let you know what is happening. Sure its time consuming, but it will tell you your problem. As I see your code, things look fine. I could be missing something (since I missed the preserve the first time), but something also may be messing up right before the if statement.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top