Changing dictionary entries with a structure in value in Visual Basic 2012
source link: https://www.codesd.com/item/changing-dictionary-entries-with-a-structure-in-value-in-visual-basic-2012.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Changing dictionary entries with a structure in value in Visual Basic 2012
I have this structure:
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
I am declaring the dictionary in the following way:
Private MySourceDBDictionary As New Dictionary(Of Integer, SourceDB)
Now values get added to the dictionary...
At some point I want to change values of the dictionary in an for each loop:
For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
MySourceDBDictionary.Item(element.Key).LastTransmittedId = 0
Next
For some reason this does not work...
The error code is: Expression is a value and therefore cannot be the target of an assignment.
How its it possible, to change already existing values in a dictionary???
Kind Regards! and Thanks.
What you want to do cannot be done with a value type but you can do what you want by reading the Structure out and passing it back.
For Each element As KeyValuePair(Of Integer, SourceDB) In MySourceDBDictionary
Dim src = MySourceDBDictionary(element.Key)
src.LastTransmittedId = 0
MySourceDBDictionary(element.Key) = src
Next
This replaces the complete value type with a new 'value'
Alternatively you can use the code you have if you use a Reference Type
instead of a Value Type
i.e. A Class
instead of a Structure
. I would suggest that this should be a Class instead of a Structure as you are modifying the values after creation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK