|
From: Bob Altman on 1 Jul 2008 16:11 Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type -- in this case, a Boolean): Dim dic As New Dictionary(Of Int32, Boolean) dic(123) = True I want to set all of the existing entries in the dictionary to False. The obvious solution: For Each value As Boolean In dic.Values value = False Next does not set the dictionary values to False as one would expect. Rather, this code gets a temp variable named value and sets that temp variable to false, leaving the dictionary value unchanged. Ok, so I try this code: For Each key As Int32 In dic.Keys dic(key) = False Next That gets me the dreaded "Collection was modified; enumeration operation may not execute." So, how can I set all of the Boolean values in this dictionary to False? TIA - Bob
From: Family Tree Mike on 1 Jul 2008 17:06 It seems to me that you should just check if the dictionary does not contain the key, assume false. You should not need to fill the dictionary with every possibility. dict.ContainsKey(123), for example. "Bob Altman" <rda(a)nospam.nospam> wrote in message news:ernaia72IHA.2424(a)TK2MSFTNGP04.phx.gbl... > Hi all, > > I'm trying to do something that should be really easy, but I can't think > of an obvious way to do it. I have a dictionary whose value is a "value > type" (as opposed to a reference type -- in this case, a Boolean): > > Dim dic As New Dictionary(Of Int32, Boolean) > dic(123) = True > > I want to set all of the existing entries in the dictionary to False. The > obvious solution: > > For Each value As Boolean In dic.Values > value = False > Next > > does not set the dictionary values to False as one would expect. Rather, > this code gets a temp variable named value and sets that temp variable to > false, leaving the dictionary value unchanged. > > Ok, so I try this code: > > For Each key As Int32 In dic.Keys > dic(key) = False > Next > > That gets me the dreaded "Collection was modified; enumeration operation > may not execute." > > So, how can I set all of the Boolean values in this dictionary to False? > > TIA - Bob >
From: Kerry Moorman on 1 Jul 2008 17:08 Bob, While we're wating for the elegant solution, here's abrute force solution: Dim myKeysArray(dic.Keys.Count - 1) As Integer dic.Keys.CopyTo(myKeysArray, 0) For Each key As Integer In myKeysArray dic(key) = False Next Kerry Moorman "Bob Altman" wrote: > Hi all, > > I'm trying to do something that should be really easy, but I can't think of an > obvious way to do it. I have a dictionary whose value is a "value type" (as > opposed to a reference type -- in this case, a Boolean): > > Dim dic As New Dictionary(Of Int32, Boolean) > dic(123) = True > > I want to set all of the existing entries in the dictionary to False. The > obvious solution: > > For Each value As Boolean In dic.Values > value = False > Next > > does not set the dictionary values to False as one would expect. Rather, this > code gets a temp variable named value and sets that temp variable to false, > leaving the dictionary value unchanged. > > Ok, so I try this code: > > For Each key As Int32 In dic.Keys > dic(key) = False > Next > > That gets me the dreaded "Collection was modified; enumeration operation may not > execute." > > So, how can I set all of the Boolean values in this dictionary to False? > > TIA - Bob > > >
From: Armin Zingler on 1 Jul 2008 16:28 "Bob Altman" <rda(a)nospam.nospam> schrieb > So, how can I set all of the Boolean values in this dictionary to > False? For Each key In dic.Keys.ToArray dic(key) = False Next Armin
From: Tom Shelton on 1 Jul 2008 18:10
On 2008-07-01, Bob Altman <rda(a)nospam.nospam> wrote: > Hi all, > > I'm trying to do something that should be really easy, but I can't think of an > obvious way to do it. I have a dictionary whose value is a "value type" (as > opposed to a reference type -- in this case, a Boolean): > > Dim dic As New Dictionary(Of Int32, Boolean) > dic(123) = True > > I want to set all of the existing entries in the dictionary to False. The > obvious solution: > > For Each value As Boolean In dic.Values > value = False > Next > > does not set the dictionary values to False as one would expect. Just a small comment, since you have received a solution - but, the observed behavior is EXACTLY what is to be expected... Booleans are value types - and so on any assignment, a copy is made. It's basically the same as: Dim b1 As Boolean = True Dim b2 As Boolean = b1 Console.WriteLine ("b1 = {0}, b2={1}", b1, b2) b1 = False Console.WriteLine ("b1 = {0}, b2={1}", b1, b2) -- Tom Shelton |