Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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 在Visual Basic 2012中更改以结构作为值的词典条目_Vb.net_Visual Studio 2012_Dictionary - Fatal编程技术网

Vb.net 在Visual Basic 2012中更改以结构作为值的词典条目

Vb.net 在Visual Basic 2012中更改以结构作为值的词典条目,vb.net,visual-studio-2012,dictionary,Vb.net,Visual Studio 2012,Dictionary,我有这样的结构: Public Structure SourceDB Public SourceDBid As Integer Public SourceDBWimLocationName As String Public SourceDBDBName As String Public SourceDBIPAdress As String Public SourceDBPort As String Public SourceDBUsername A

我有这样的结构:

Public Structure SourceDB
    Public SourceDBid As Integer
    Public SourceDBWimLocationName As String
    Public SourceDBDBName As String
    Public SourceDBIPAdress As String
    Public SourceDBPort As String
    Public SourceDBUsername As String
    Public SourceDBPassword As String
    Public ConnectivityState As String
    Public LastTransmittedId As Integer
End Structure
我以以下方式声明词典:

Private MySourceDBDictionary As New Dictionary(Of Integer, SourceDB)
现在值被添加到字典中

在某个时刻,我想更改for each循环中字典的值:

For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
    MySourceDBDictionary.Item(element.Key).LastTransmittedId = 0
Next
由于某种原因,这不起作用

错误代码为:表达式是一个值,因此不能作为赋值的目标

如何更改字典中已有的值


亲切的问候!谢谢

您想要做的事情不能用值类型来完成,但您可以通过读取结构并将其传回来完成

    For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
        Dim src = MySourceDBDictionary(element.Key)
        src.LastTransmittedId = 0
        MySourceDBDictionary(element.Key) = src
    Next
这将用新的“值”替换完整的值类型


或者,如果您使用的是
参考类型
而不是
值类型
,即
而不是
结构
,则可以使用您拥有的代码。我建议这应该是一个类,而不是一个结构,因为您在创建后修改值

您想要做的事情不能用值类型来完成,但您可以通过读取结构并将其传回来完成

    For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
        Dim src = MySourceDBDictionary(element.Key)
        src.LastTransmittedId = 0
        MySourceDBDictionary(element.Key) = src
    Next
这将用新的“值”替换完整的值类型


或者,如果您使用的是
参考类型
而不是
值类型
,即
而不是
结构
,则可以使用您拥有的代码。我建议这应该是一个类,而不是一个结构,因为您在创建后修改了值

上面的问题应该很好地解释了什么时候应该使用
Structure

将结构更改为
,代码应该可以工作, 您还可以将foreach循环稍微更改为这样的内容:

    For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
        element.Value.LastTransmittedId = 0
    Next

问题在于结构。看看这个问题

上面的问题应该很好地解释了什么时候应该使用
Structure

将结构更改为
,代码应该可以工作, 您还可以将foreach循环稍微更改为这样的内容:

    For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
        element.Value.LastTransmittedId = 0
    Next