Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用字典的键更新值_Vb.net_Vb.net 2010 - Fatal编程技术网

Vb.net 使用字典的键更新值

Vb.net 使用字典的键更新值,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我正在VB.NET Windows应用程序中使用词典 我在字典中添加了几个值,我想用它们的键编辑一些值 例如: 下面是一个数据表,我想将键“DDD”的值更新为1 如何做到这一点 For Each kvp As KeyValuePair(Of String, String) In Dictionary1 If i = value And kvp.Value <> "1" Then NewFlat = kvp.Key.ToString ------

我正在VB.NET Windows应用程序中使用
词典

我在
字典中添加了几个值,我想用它们的键编辑一些值

例如: 下面是一个数据表,我想将键“DDD”的值更新为1

如何做到这一点

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
字典1中每个kvp作为KeyValuePair(字符串、字符串)的

如果i=值和kvp.值“1”,则
NewFlat=kvp.Key.ToString
---------------------------------------------
我想更新设置各个键的值1。
我应该在这里写什么?
---------------------------------------------
IsAdded=True
退出
如果结束
i=i+1
下一个kvp

如果您知道要更改哪个kvp的值,则不必对每个kvp迭代(
)字典。要将“DDD”/“0”更改为“DDD”/“1”:


无法使用KeyValuePair,因为在数据被修改时更新它会导致错误。

如果您尝试为每个
循环修改
中的任何集合,您将得到一个,
InvalidOperationException
。一旦集合更改,枚举数(每个
变量的
)将变为无效。尤其是对于字典,这是不需要的:

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
方法A无法编译,因为
属性是只读的。
方法B将更改计数/值,但会导致
Next
异常,因为
kvp
不再有效

字典有一个内置的方法来为您完成所有这些:

If myDict.ContainsKey(searchKey) Then
    myDict(searchKey) = "1"
End If

使用该键可从字典中获取/设置/更改/删除。

有时您确实希望对字典中的每个项目执行某些操作。例如,我使用字典来存储相当大的数据结构,这仅仅是因为从一个巨大的堆中获取数据几乎是瞬间的,即使字典看起来是40MB的RAM

例如:

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

因为您没有更改正在枚举的内容,所以不会引发异常。当然,如果出现其他问题并弄乱了您的数据,那么您可能无法遍历所有内容,或者无法在集合中找到密钥,这取决于发生了什么。我只为在自己的机器上进行繁重的处理而编写这种东西。

无法使用KeyValuePair,因为在数据被修改时更新后会出现错误。如果您正确解释您的确切条件(输入+您想要得到的),我相信Tim或我都可以提供一个代码,完全满足您的要求。请关注一个问题,删除另一个问题,下一次,如果你的担心没有得到正确的解决,你应该考虑选择(或更好地解释你的问题)而不是发布一个新的问题。这似乎不回答如何编辑给定键的值的问题。这是如何编辑给定所有关键点的所有值。
If myDict.ContainsKey(searchKey) Then
    myDict(searchKey) = "1"
End If
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