vb.net 使用字典的键更新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19539367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating Value using key of the dictionary
提问by Nilesh B
I am using a Dictionary
in VB.NET Windows application.
我Dictionary
在 VB.NET Windows 应用程序中使用。
I have added several values in a Dictionary
and I want to edit some values using their key.
我在 a 中添加了几个值,Dictionary
我想使用它们的键编辑一些值。
Example: Below we have a DATA table and I want to update the value of the key - "DDD" to 1
示例:下面我们有一个 DATA 表,我想将键的值 - “DDD”更新为 1
AAA - "0" BBB - "0" CCC - "0' DDD - "0"
AAA - "0" BBB - "0" CCC - "0' DDD - "0"
How can this be done?
如何才能做到这一点?
For Each kvp As KeyValuePair(Of String, String) In Dictionary1
If i = value And kvp.Value <> "1" Then
NewFlat = kvp.Key.ToString
---------------------------------------------
I want to update set the Value 1 of respective key.
What should I write here ?
---------------------------------------------
IsAdded = True
Exit For
End If
i = i + 1
Next kvp
回答by ??ssa P?ngj?rdenlarp
If you know which kvp's value you want to change, you do not have to iterate (for each kvp
) the dictionary to so. to Change "DDD"/"0" to "DDD"/"1":
如果您知道要更改哪个 kvp 的值,则不必重复 ( for each kvp
) 字典。将“DDD”/“0”更改为“DDD”/“1”:
myDict("DDD") = "1"
cant use the KeyValuePair its gives error after updating it as data get modified.
cant use the KeyValuePair its gives error after updating it as data get modified.
If you try to modify anycollection in a For Each
loop, you'll get a, InvalidOperationException
. The enumerator (the For Each
variable) becomes invalid once the collection changes. Especially with a Dictionary, this is not needed:
如果您尝试在循环中修改任何集合For Each
,您将得到一个, InvalidOperationException
。For Each
一旦集合发生变化,枚举器(变量)就会失效。特别是对于字典,这是不需要的:
Dim col As New Dictionary(Of String, Int32)
col.Add("AAA", 0)
...
col.Add("ZZZ", 0)
Dim someItem = "BBB"
For Each kvp As KeyValuePair(Of String, Int32) In col
If kvp.Key = someItem Then
' A) Change the value?
vp.Value += 1 ' will not compile: Value is ReadOnly
' B) Update the collection?
col(kvp.Key) += 1
End If
Next
Method A wont compile because the Key
and Value
properties are ReadOnly.
Method B will change the count/Value, but result in the Exception on Next
because kvp
is no longer valid.
方法 A 不会编译,因为Key
和Value
属性是只读的。
方法 B 将更改计数/值,但导致 Exception onNext
因为kvp
不再有效。
Dictionaries have a built in method to do all that for you:
字典有一个内置的方法来为你做这一切:
If myDict.ContainsKey(searchKey) Then
myDict(searchKey) = "1"
End If
Use the key to get/set/change/remove from the dictionary.
使用键从字典中获取/设置/更改/删除。
回答by Justin Neville-Rolfe
There are times when you really do want to do something on each item in a dictionary. For example, I use dictionaries to store quite large structures of data, simply because it seems to be near instantaneous to grab my data from a huge heap, even when the dictionary appears to be 40MB of RAM.
有时您确实想对字典中的每个项目做一些事情。例如,我使用字典来存储相当大的数据结构,仅仅是因为从一个巨大的堆中抓取我的数据似乎几乎是即时的,即使字典看起来有 40MB 的 RAM。
For example:
例如:
dim col as new dictionary (of string, myStructData)
dim colKeys() as string = col.keys.toArray()
for each colKey in colKeys
dim tempVal as new myStructData= col(colKey)
'do whatever changes you want on tempVal
col(colKey)=tempVal
next colKey
Because you are not changing what you're enumerating over, the exception is not thrown. Of course, if something else came along and messed up your data, then you'd either not iterate over everything, or you'd not find the key in the collection, depending on what had happenned. I only write this sort of thing for heavy processing on my own machine.
因为您没有更改要枚举的内容,所以不会抛出异常。当然,如果其他事情出现并弄乱了您的数据,那么您要么不迭代所有内容,要么在集合中找不到键,这取决于发生了什么。我只写这种东西是为了在我自己的机器上进行繁重的处理。