Two articles on releasing a session (2)

Source: Internet
Author: User
Tags naming convention
Deleting a Subset of Session Variables
When using Sessions to store variables in, I use a naming convention-for example, for all Customer related info I prefix the session variable with the substring Customer. (so for the CustomerID it wocould be Customer. ID, the customer username wocould be Customer. name, etc .) this is very useful when viewing the session objects as you can see the related objects straight off.
The problem I had the other day was that I wanted to remove only those items from the Session which were prefixed SW .. So first off I used the following code:
'---------------------------------------
Session ("SW.1") = "Test 1"
Session ("SW.2") = "Test 2"
Session ("Other") = "Other"
For Each SessionItem in Session. Contents
If Left (SessionItem, 3) = "SW." then
Session. Contents. Remove (SessionItem)
End if
Next
'---------------------------------------
This seems fine, but when it's run, what happens is that SW.1 is removed, but SW.2 is NOT removed. Why? I'm not exactly sure, but I guess that the index is then reset so that SW.2 is now where SW.1 was, and seeing as we have iterated past that item in the For Each... next statement, the loop just moves to Other, missing out SW.2 altogether! Eek!
So to get round this I wrote the following function, which will properly delete all Session variables that begin with a specified substring:
'---------------------------------------
Function SessionRemoveSelected (sItemPrefix)
'/////////////////////////////////////// //////////
'Remove Selected Items starting with sItemPrefix
'From the Session. e.g. SS. will remove SS. ID and
'SS. NAME but not mermerid Returns True or False
'Depending on whether any items where removed.
'---------------------------------------
'Sitemprefix [string]: Item Prefix
'/////////////////////////////////////// //////////

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.