Solved

Dictionary and a struct

Posted on 2013-01-16
4
487 Views
Last Modified: 2013-01-16
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
Comment
Question by:jdcoburn
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 167 total points
ID: 38783859
Why don't you use the ConcurrentDictionary class?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 167 total points
ID: 38784128
"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
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 166 total points
ID: 38784277
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
 

Author Closing Comment

by:jdcoburn
ID: 38785087
thanks. all the solutions added value in different ways.<br />Jim
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question