Question

J'ai un code comme ceci:

Dim col As Collection = New Collection
col.Add(value1, "key1")
col.Add(value2, "key2")

' later...
For Each item As String In col
    ' want to get valueX and keyX here; currently, "item" holds the value
Next

Comment puis-je obtenir à la fois la valeur et la clé dans la boucle? Peut-être il y a une autre classe qui rend ce plus facile?

Était-ce utile?

La solution

j'utiliser un dictionnaire générique ...

 Imports System.Collections.Generic  'at top of file

    Dim col As New Dictionary(Of String, Of Object) 'or whatever type
    col.Add("key1", value1)
    col.Add("key2", value2)    

    For Each item as KeyValuePair(of String, Object) in col
           Console.WriteLine(item.key & ": " & item.value)
    Next
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top