Accessing a Value in Dictionary with Array

Mac Desktop

Var aStrings() As String
aStrings.ResizeTo(100)
Var someString As String = "hello"
Var theDictionary As New Dictionary

theDictionary.Value("arrayString") = aStrings
theDictionary.Value("arrayString")(0) = someString

The last line is not accepted. There is more than one method with this name but this does not match any of the available signatures. What am I doing wrong?

I can put an array into a Dictionary with its own key (In this case “arrayString”). But how can I get a reference to a single value from an array that is situated in that Dictionary?

theDictionary.Value(“arrayString”) = aStrings

I suspect that my problem is that this line does not create a “new” array in the Dictionary. It is probably just storing a reference to the aString array. If that is the case, I guess my question is how do you create an array that lives as a value in a Dictionary? And an array that I can access its elements.

I think you need to copy the array from the dictionary into a new array, manipulate the values and get the updated array into the dictionary.

1 Like

Looking around, I came across this:

From Stephane Mons elsewhere in the Forum.
An array stored in a Dictionary is actually a reference to the array (or any other Object). It does not matter whether you get the reference from the Dictionary or you have a copy of it somewhere: any modification will be seen by both.

So when you have a Dictionary with one of its values being an array, you really only have a reference to an array in the Dictionary and the language is constructed so that you cannot change the elements of the array directly. You can only retrieve or change the values in the array that is being referenced. Not directly from the Dictionary (As Beatrix implies in her note).

To me it is weird that if the Dictionary is a Global and the array to which it is a reference is some local variable and goes out of scope, you still have the data preserved in the Dictionary but you cannot touch it directly. It is sort of locked in there.

You can create a local variable in your current environment

Var localStringArray() As String
localStringArray = theDictionary.Value("arrayString")

and now you can use localStringArray to look at or change the individual elements in the theDictionary.Value(“arrayString”). I guess this is the approach that I will use although it seems awkward to me and I wish there were another way.

It is odd to me that the syntax:

theDictionary.Value("arrayString")(0)

is not allowed.