0

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

4
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented May 4, 2015 at 21:10
  • "doesn't seem to be working" is not a clear problem description. In what way does it seem to not be working? How did you determine it was not working, and how did you expect it to work? Commented May 4, 2015 at 21:10
  • Well 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. Commented May 4, 2015 at 21:12
  • 2
    Please click the "edit" button and update your question with this information. That sort of information belongs in the question, not in the comments. Commented May 4, 2015 at 21:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.