0

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")
1
  • 1
    Have you tried using Dictionary type? Commented Oct 2, 2014 at 9:39

2 Answers 2

4

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.

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

Comments

1

What you want is a StringDictionary

http://msdn.microsoft.com/en-us/library/system.collections.specialized.stringdictionary.add(v=vs.110).aspx

  Dim myCol As New StringDictionary()
  myCol.Add("red", "rojo")

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.