Reputation: 337
I have a few variables called: _2sVal, _3sVal, _4sVal, etc I want to change each of their values through a loop. Like:
For i = 1 To 10
'set the value
Next
I've tried putting them in a dictionary like:
Dim varDict As New Dictionary(Of String, Integer)
varDict.Add("2sVal", _2sVal)
varDict.Add("3sVal", _3sVal)
varDict.Add("4sVal", _4sVal)
I can retrieve the value using
MsgBox(varDict(i.ToString & "sVal"))
But when I try to change it like
varDict(i.ToString & "sVal") = 5
It doesn't do anything. No errors or exceptions either, just the value stays unchanged
Upvotes: 0
Views: 300
Reputation: 4699
When you are using
varDict.Add("4sVal", _4sVal)
You are not putting the _4sVal
variable inside the dictionary, but its value.
Then, changing the dictionary will not change the _4sVal
, since there is no reference of it inside the dictionary.
What I mean is
varDict("4sVal") = 5
will change the value of dictionary but not the variable _4sVal
itself.
I think the correct to do is define that variables as Properties, defined like:
Property _4sVal As Integer
Get
Return varDict("4sVal")
End Get
Set(value As Integer)
varDict("4sVal") = value
End Set
End Property
This way you will not have to change anything in the rest of your code. It will be transparent.
Upvotes: 1