0

Apologies if this is a duplicate, but can't find anything that matches everything I need. I have an object, one of the members of which is a Dictionary object, defined as follows:

public class MyTestObject
{
 public int index { get; set; }
 // various other members here
 public Dictionary<string, bool> MyTestValues { get; set; }
}

I have a list of class MyTestObject:

List<MyTestObject> values = new List<MyTestObject>();

I have a loop that runs through each instace of values and a sub-loop which runs through the KVP in the dictionary

foreach (MyTestObject current in values)
{
 foreach (KeyValuePair<string, bool> testCurrent in current.MyTestValues)
 {
 }
}

Within the 2nd loop I need to change boolean value of each of the Dictionary items and this is where I am falling down.

Ideally I'd like to be able to do something similar to the following:

values[current.index].MyTestValues.Where(t => t.Key == testCurrent.Key).Single().Value = true;

i.e., it's using the index member to access the field in the object and update the value. Unfortunately this doesn't work as it says the Value field is read only.

I'm sure I'm getting confused unnecessarily here. Any advice appreciated

asked Dec 6, 2013 at 16:05
6
  • 1
    It seems like you could just use values[current.index].MyTestValues[testCurrent.Key] = true unless I'm misunderstanding something? Commented Dec 6, 2013 at 16:08
  • I am falling down. What's the problem? Commented Dec 6, 2013 at 16:09
  • @hamlet - the Value field is read only. Commented Dec 6, 2013 at 16:10
  • @Andrew Don't try and modify the KeyValuePairs directly. Access the items in the dictionary through the indexer. Commented Dec 6, 2013 at 16:13
  • You're trying to modify the contents of the dictionary in a foreach which isn't valid? See this question: stackoverflow.com/questions/2260446/… Commented Dec 6, 2013 at 16:15

1 Answer 1

3

Just do:

values[current.index].MyTestValues[testCurrent.Key] = true;

It will update the existing value and if the key doesn't exist, it will add the new item in the dictionary with the new key.

answered Dec 6, 2013 at 16:14
Sign up to request clarification or add additional context in comments.

4 Comments

@Magus, in that case it is better to check for Key using ContainsKey
My comment was dumb. You are correct. ContainsKey is the best way to do a check.
Hmmmm...I get an index out of range, which doesn't make much sense. I will check further, but I think this is the way to go.
Ignore. Me being an idiot and setting the value of index to 1 instead of 0 :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.