Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2005, 12:21 AM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to edislasm
Default Problem doing Session Contents Iteration

Hi, I'm trying to make a loop that goes through all Session variables and deletes the ones with the prefix "Caract". This is my code:

    for each variable in Session.Contents()
        If left(variable,6) = "Caract" Then
            Session.Contents.Remove(variable)
        End If
    next

I've checked the code many times and it seems to be ok. The problem is that only 1 item is removed. I mean, if I have 5 variables with the prefix I would have to reload the page 5 times to get them all removed. What could be happnin'?

Eduardo Islas
Mexico

 
Old August 4th, 2005, 10:50 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I am not 100% sure, but I think this is caused by the fact that you're modifying a collection (using .Remove) while iterating over it (using For Each). This is not allowed, and usually you'll get an error when you try to do it.

You can fix this by using a regular For loop with a counter. Can't recall if Session.Contents implements a Count property, but if it does, you can use that. E.g.

For i = 0 To Session.Contents().Count - 1
  ' Code to remove goes here
Next


If that doesn't help / work, store the names of the items you want to remove in a separate array, and remove them after you have finished with the For Each loop.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old August 4th, 2005, 12:03 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to edislasm
Default

Thanks Imar.
The iteration works, Session.Contents does have a Count property. But before, I could get the name of the session variable using the word "variable" created in the for-each. Now how can I get the name of the variable if Session.Contents(i) returns the value and not the name?:

    for i = 1 to Session.Contents().count
        variablename = 'I need to get the property here
        If left(variablename,6) = "Caract" Then
            Session.Contents.Remove(i)
        End If
    next


 
Old August 4th, 2005, 01:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I see what you mean;,Interesting problem. I don't think you can. AFAIK, the Contents(i) object does not have a key you can retrieve, as you'd maybe expect.

So, I am afraid this leaves you with the other option: store the keys in an array using For Each, and then use a normal For loop to delete the items.

Sorry if I put you on the wrong track...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old August 4th, 2005, 01:44 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to edislasm
Default

Well, i noticed that with the for-each, variables are all deleted. It worked from the begining lol!, the problem is somewhere in the code I was using to check it. Tanks anyway!






Similar Threads
Thread Thread Starter Forum Replies Last Post
iteration bostek Excel VBA 5 August 30th, 2006 01:23 AM
ASP SESSION CONTENTS VARIABLES CAN'T BE CHANGED Epevas Classic ASP Components 8 January 30th, 2006 09:38 AM
Picture iteration bahachin Excel VBA 3 September 28th, 2005 12:44 PM
session and cookie problem (empty session file) msincan BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 February 27th, 2005 05:31 PM
Iteration through a dropdown. tryxxter PHP How-To 1 February 9th, 2005 11:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.