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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Manipulating a hashtable

Manipulating a hashtable

Scheduled Pinned Locked Moved Visual Basic
questionannouncement
3 Posts 3 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.
  • W Offline
    W Offline
    woaksie
    wrote on last edited by
    #1

    I am trying to iterate through a hashtable using an enumerator. I want to change some values as I go. Here is a simplified version of my code…. ptr = hashTab.GetEnumerator While ptr.MoveNext hashTab.Item(ptr.Key) = “something different” End While I get the following exception when I change a value – Collection was modified; enumeration operation may not execute. What is the correct way to perform this type of operation? John.

    S N 2 Replies Last reply
    0
    • W woaksie

      I am trying to iterate through a hashtable using an enumerator. I want to change some values as I go. Here is a simplified version of my code…. ptr = hashTab.GetEnumerator While ptr.MoveNext hashTab.Item(ptr.Key) = “something different” End While I get the following exception when I change a value – Collection was modified; enumeration operation may not execute. What is the correct way to perform this type of operation? John.

      S Offline
      S Offline
      Steven Campbell
      wrote on last edited by
      #2

      The problem occurs because you are completely replacing the item in the hashtable. This is not allowed during an enumeration. If your hashtable contains objects, it is allowed to update properties of those objects. So one solution is to create an object (or type) that contains the string value. Another solution is to build a new collection, and then discard the old one.


      my blog

      1 Reply Last reply
      0
      • W woaksie

        I am trying to iterate through a hashtable using an enumerator. I want to change some values as I go. Here is a simplified version of my code…. ptr = hashTab.GetEnumerator While ptr.MoveNext hashTab.Item(ptr.Key) = “something different” End While I get the following exception when I change a value – Collection was modified; enumeration operation may not execute. What is the correct way to perform this type of operation? John.

        N Offline
        N Offline
        normanordas
        wrote on last edited by
        #3

        Hashtable belongs to readonly dictionary base object. Thu editing the content is not possible.what you can do ir remove the key and add it again with the new value: hashTab.Remove ptr.Key then... hashTab.Add ptr.Key, “something different”

        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