vb.net에서 각각에 대해 컬렉션 키에 어떻게 액세스합니까?
-
13-09-2019 - |
문제
다음과 같은 코드가 있습니다.
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
루프 내에서 값과 키를 모두 얻으려면 어떻게해야합니까? 어쩌면 이것을 더 쉽게 만드는 또 다른 수업이 있습니까?
해결책
일반 사전을 사용할 것입니다 ...
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
제휴하지 않습니다 StackOverflow