Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable problem

Hashtable problem

Scheduled Pinned Locked Moved C#
help
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yboc
    wrote on last edited by
    #1

    Hashtable approval = new Hashtable(); //then I added some keys to it and set the value to "" //I need to set its value to "" again by below foreach foreach( object obj in approval.Keys ) { approval[obj] = ""; } //But it said that "Collection was modified; enumeration operation may not execute." //Pls help me to make this possible. Thanks a lot for your attention.

    A 1 Reply Last reply
    0
    • Y yboc

      Hashtable approval = new Hashtable(); //then I added some keys to it and set the value to "" //I need to set its value to "" again by below foreach foreach( object obj in approval.Keys ) { approval[obj] = ""; } //But it said that "Collection was modified; enumeration operation may not execute." //Pls help me to make this possible. Thanks a lot for your attention.

      A Offline
      A Offline
      Alexander Kojevnikov
      wrote on last edited by
      #2

      MSDN - foreach: The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects. MSDN - Hashtable.Keys Property: The returned ICollection is not a static copy; instead, the ICollection refers back to the keys in the original Hashtable. Therefore, changes to the Hashtable continue to be reflected in the ICollection. An alternative approach could looke like this:

      Hashtable ht = new Hashtable();
      ht.Add("one", 1);
      ht.Add("two", 2);
      ht.Add("three", 3);
      ht.Add("four", 4);

      ICollection keys = ht.Keys;
      object[] copiedKeys = new object[keys.Count];
      keys.CopyTo(copiedKeys, 0);
      foreach(object key in copiedKeys)
      {
      ht[key] = "";
      }

      Alexandre Kojevnikov MCAD charter member Leuven, Belgium

      Y 1 Reply Last reply
      0
      • A Alexander Kojevnikov

        MSDN - foreach: The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects. MSDN - Hashtable.Keys Property: The returned ICollection is not a static copy; instead, the ICollection refers back to the keys in the original Hashtable. Therefore, changes to the Hashtable continue to be reflected in the ICollection. An alternative approach could looke like this:

        Hashtable ht = new Hashtable();
        ht.Add("one", 1);
        ht.Add("two", 2);
        ht.Add("three", 3);
        ht.Add("four", 4);

        ICollection keys = ht.Keys;
        object[] copiedKeys = new object[keys.Count];
        keys.CopyTo(copiedKeys, 0);
        foreach(object key in copiedKeys)
        {
        ht[key] = "";
        }

        Alexandre Kojevnikov MCAD charter member Leuven, Belgium

        Y Offline
        Y Offline
        yboc
        wrote on last edited by
        #3

        Thanks a lot. It is a good idea.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups