Wrox Programmer Forums
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
|
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 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
 
Old April 6th, 2010, 12:15 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
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)
 
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
 
Old April 6th, 2010, 02:37 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
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)
 
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
 
Old April 8th, 2010, 12:12 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
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)
 
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





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





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