Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 7th, 2009, 04:59 PM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default BizObject PrugeCacheItems method

Hi,

I don't know if this question has been posted before, but I couldn't find it. I am looking at the
Code:
BizObject.PurgeCacheItems()
static method (page 108 of the book) and I am wondering if there is any way to avoid using two loops to do a task.

The first loop (the while loop) fills a list and then the second uses the list to purge the cache. I am thinking that using the enumerator we cannot remove the item directly in the while loop because that will set the enumerator out of sync with the elements in the dictionary. However, is there not another way to loop through the elements of the dictionary so that you can remove the elements in one single loop?

Thanks!
 
Old August 11th, 2009, 10:39 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

According to MSDN (regarding the GetEnumerator method)

"Items can be added to or removed from the cache while this method is enumerating through the items."

If so, seems to me this would work:

Code:
protected static void PurgeCacheItems(string prefix)
{
   IDictionaryEnumerator enumerator = Cache.GetEnumerator();
   while (enumerator.MoveNext())
   {
      if (enumerator.Key.ToString().ToLower().StartsWith(prefix.ToLower()))
      {
         Cache.Remove(enumerator.Key.ToString());
      }               
   }
}
However, like you said, it was always my understanding that deleting elements from a collection invalidates the enumerator, in which case an InvalidOperationException would be thrown. Hmm... have to try it now...

EDIT: Just tried it. seems to work fine.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}

Last edited by Lee Dumond; August 11th, 2009 at 10:49 PM..
The Following User Says Thank You to Lee Dumond For This Useful Post:
alfinmass (August 12th, 2009)
 
Old August 12th, 2009, 08:16 AM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Thanks Lee, that was exactly the code I thought of first, and then somehow I thought it wouldn't work because as it iterates forward it would lose it's 'counting'. My next thought was if there was a way to iterate backwards (starting at the last item and finishing in the first) through the cache, because that way if you remove an item of the collection you would not lose your 'count'.

Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about BizObject class. yevi BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 10 April 16th, 2009 01:12 PM
BizObject class - Intellisense? ViagraFalls BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 September 19th, 2007 02:24 PM
Thoughts on BizObject hpox BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 7 June 9th, 2007 07:47 AM
Why is Cache Property used in BizObject ? Scott663 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 March 8th, 2007 10:47 PM
Can you give me an example of BizObject content ? dwfresh BOOK: ASP.NET Website Programming Problem-Design-Solution 0 December 19th, 2003 04:10 AM





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