• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Dictionary and a struct

hi -- I'm using C# and VS2010 with .net 4.
i have a dictionary that i use to keep state information in my program. the value part of the key value pair is a struct (called "node"). it seems that i can only update "node" as a single entity rather than updating individual elements when i have fresh data. i have the additional problem of the data coming from two different threads. i set up the dictionary as static and use flags to protect the write operation. but every now and then (every couple hours) i get an enumeration error, which i suspect is related to the update process.
is there a better approach?
thanks,
0
jdcoburn
Asked:
jdcoburn
3 Solutions
 
käµfm³d 👽Commented:
Why don't you use the ConcurrentDictionary class?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"it seems that i can only update "node" as a single entity rather than updating individual elements when i have fresh data."

Switch from a struct to a class.
0
 
anarki_jimbelCommented:
About structs, many people make the same mistake by forgetting that structures are value type. See more details here, e.g.:

http://stackoverflow.com/questions/7126318/how-do-you-assign-values-to-structure-elements-in-a-list-in-vb-net


Use class instead.

ConcurrentDictionary should be a good solution but I have a feeling that something else happens.
0
 
jdcoburnAuthor Commented:
thanks. all the solutions added value in different ways.<br />Jim
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now