Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2009, 04:59 PM
Authorized User
 
Join Date: Aug 2009
Location: Brookline, MA
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!
  #2 (permalink)  
Old August 11th, 2009, 10:39 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
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)
  #3 (permalink)  
Old August 12th, 2009, 08:16 AM
Authorized User
 
Join Date: Aug 2009
Location: Brookline, MA
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!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:52 PM.


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