Reputation: 2073
Is there a way to insert values into an arraylist and assign them with a string as key/index?
I tried:
arrayList.Insert("abc", "value123")
Upvotes: 0
Views: 2635
Reputation: 346
What you want is a StringDictionary
Dim myCol As New StringDictionary()
myCol.Add("red", "rojo")
Upvotes: 1
Reputation: 460380
First, there is no need to use an ArrayList
anymore. Use a typed List(Of T)
to avoid that you always have to cast the objects which is also more error-prone.
In this case it seems that you actually need a Dictionary(Of String, String)
:
Dim dict As New Dictionary(Of String, String)
dict.Add("abc", "value123")
Now you can access it by key very fast:
Dim value As String = dict("abc") ' exception if it doesnt contain this key '
Note that the keys must be unique and that you can use TryGetValue
or ContainsKey
to check whether or not it contains the key to avoid the exception.
Upvotes: 4