Come posso accedere chiavi Collection durante Ad Ciascuno in VB.net?
-
13-09-2019 - |
Domanda
Ho qualche codice come questo:
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
Come posso ottenere sia il valore e la chiave all'interno del ciclo? Forse c'è un'altra classe che rende questo più facile?
Soluzione
mi piacerebbe utilizzare un dizionario generico ...
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow