Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
Password Reminder
Register
| 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 April 6th, 2010, 11:41 AM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default PurgeCacheItems method - wouldn’t the following approach be more efficient?

Author uses the following code to purge cached items:

Code:
     
      protected static void PurgeCacheItems(string prefix)
      {
         prefix = prefix.ToLower();
         List<string> itemsToRemove = new List<string>();
         
         IDictionaryEnumerator enumerator = BizObject.Cache.GetEnumerator();
         while (enumerator.MoveNext())
         {
            if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
               itemsToRemove.Add(enumerator.Key.ToString());
         }

         foreach (string itemToRemove in itemsToRemove)
            BizObject.Cache.Remove(itemToRemove);
}

Is there a reason why we couldn’t instead use the following code to purge cached items:

Code:
      
       protected static void PurgeCacheItems(string prefix)
      {
         prefix = prefix.ToLower();

          foreach(DictionaryEntry item in Cache)
          {
              if(item.Key.ToString().ToLower().StartsWith(prefix))
                  BizObject.Cache.Remove(item.Key.ToString(); 
          }
       }
If both approaches are fine, then why did the author decide to use the more complicated approach?

I would appreciate a bit of help
  #2 (permalink)  
Old April 6th, 2010, 12:15 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

Because you can't remove items from a collection while you are iterating over it.

Here the author presents is a common way to work around that. What you do is to create a temporary collection, and add to it all the items you want to remove. Then, you iterate over the temp collection while removing items from the original collection.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
asponmynet (April 8th, 2010)
  #3 (permalink)  
Old April 6th, 2010, 02:18 PM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
Because you can't remove items from a collection while you are iterating over it.
* Strange. I'm able to modify items from an array while I'm iterating over this array, but if I try to remove an item from a collection, I get an exception.

* I assume the following code will do the job:

Code:
      protected static void PurgeCacheItems(string prefix)
      {
         prefix = prefix.ToLower();
         List<string> itemsToRemove = new List<string>();
         
         foreach(DictionaryEntry item in Cache)
         {
             if(item.Key.ToString().ToLower().StartsWith(prefix))
                 itemsToRemove.Add(enumerator.Key.ToString());
          }

          foreach (string itemToRemove in itemsToRemove)
              BizObject.Cache.Remove(itemToRemove);
      }
thank you sir
  #4 (permalink)  
Old April 6th, 2010, 02:37 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

Not at all strange, if you understand how enumeration works. Part of it is that the runtime has to keep track of how many items it is iterating over. It grabs that count at the beginning, which is why it is an exception to alter the number of items while enumerating.

Yeah, I think the code you have here will work. You could strip the code down even more by using the yield keyword.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
asponmynet (April 8th, 2010)
  #5 (permalink)  
Old April 8th, 2010, 12:04 PM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Lee Dumond View Post
Not at all strange, if you understand how enumeration works. Part of it is that the runtime has to keep track of how many items it is iterating over. It grabs that count at the beginning, which is why it is an exception to alter the number of items while enumerating.
I assumed that since we can modify item in an array while iterating through it, that same would be true for collections.

your help is much appreciated
  #6 (permalink)  
Old April 8th, 2010, 12:12 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

You can modify the collection, as long as you don't change the number of items (which deleting does).
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
asponmynet (April 12th, 2010)
  #7 (permalink)  
Old April 12th, 2010, 03:38 PM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

much appreciated
 


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
ADVICE WANTED: Which method is more efficient? kwilliams ASP.NET 2.0 Basics 3 August 31st, 2006 11:02 AM
Most efficient overriding technique jcsdeveloper C# 4 December 7th, 2005 04:18 PM
Need help with approach rickyc1 Classic ASP Databases 4 July 14th, 2005 10:26 AM
Database search - more efficient way? SoC Classic ASP Basics 4 August 15th, 2004 08:56 PM
Paging Again Still Not Efficient alyeng2000 ASP.NET 1.0 and 1.1 Basics 0 March 4th, 2004 08:36 AM



All times are GMT -4. The time now is 10:59 PM.


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