如何从集合中获取键和值 VB.Net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7499989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the Key and Value from a Collection VB.Net
提问by Troy Mitchel
How do you get the key value from a vb.net collection when iterating through it?
迭代时如何从 vb.net 集合中获取键值?
Dim sta As New Collection
sta.Add("New York", "NY")
sta.Add("Michigan", "MI")
sta.Add("New Jersey", "NJ")
sta.Add("Massachusetts", "MA")
For i As Integer = 1 To sta.Count
Debug.Print(sta(i)) 'Get value
Debug.Print(sta(i).key) 'Get key ?
Next
回答by Dan F
Pretty sure you can't from a straight Microsoft.VisualBasic.Collection.
很确定你不能直接从Microsoft.VisualBasic.Collection。
For your example code above, consider using a System.Collections.Specialized.StringDictionary. If you do, be aware that the Add method has the parameters reversed from the VB collection - key first, then value.
对于上面的示例代码,请考虑使用System.Collections.Specialized.StringDictionary。如果这样做,请注意 Add 方法具有与 VB 集合相反的参数 - 首先是键,然后是值。
Dim sta As New System.Collections.Specialized.StringDictionary
sta.Add("NY", "New York")
'...
For Each itemKey in sta.Keys
Debug.Print(sta.Item(itemKey)) 'value
Debug.Print(itemKey) 'key
Next
回答by tcarvin
I don't recommend using the Collection class, as that is in the VB compatibility library to make migrating VB6 programs easier. Replace it with one of the many classes in the System.Collections or System.Collections.Generic namespace.
我不建议使用 Collection 类,因为它位于 VB 兼容性库中,可以更轻松地迁移 VB6 程序。将其替换为 System.Collections 或 System.Collections.Generic 命名空间中的众多类之一。
回答by IvanH
It is possible to get a key with using Reflection.
使用反射可以获得密钥。
Private Function GetKey(Col As Collection, Index As Integer)
Dim flg As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
Dim InternalList As Object = Col.GetType.GetMethod("InternalItemsList", flg).Invoke(Col, Nothing)
Dim Item As Object = InternalList.GetType.GetProperty("Item", flg).GetValue(InternalList, {Index - 1})
Dim Key As String = Item.GetType.GetField("m_Key", flg).GetValue(Item)
Return Key
End Function
Not using VB.Collection is recommended but sometimes we are dealing with code when it was used in past. Be aware that using undocumented private methods is not safe but where is no other solution it is justifiable.
建议不要使用 VB.Collection,但有时我们正在处理过去使用过的代码。请注意,使用未记录的私有方法并不安全,但没有其他解决方案是合理的。
More deailed information can be found in SO: How to use reflection to get keys from Microsoft.VisualBasic.Collection
更多详细信息可以在 SO: How to usereflect to get keys from Microsoft.VisualBasic.Collection 中找到
回答by MadsHaupt
Yes, it may well, but I want recomend that you use another Collection.
是的,它可能很好,但我想建议您使用另一个集合。
How to do you do with Reflection, the type Microsoft.VisualBasic.Collection contains some private fields, the field one should use in this case is "m_KeyedNodesHash" the field, and the field type is System.Collections.Generic.Dictionary(Of String, Microsoft.VisualBasic.Collection.Node), and it contains a property called "Keys", where the return type is System.Collections.Generic.Dictionary(Of String, Microsoft.VisualBasic.Collection.Node).KeyCollection, and the only way to get a certain key is to convert it to type IEnumerable(Of String), and the call ElementAt the function.
如何使用反射,Microsoft.VisualBasic.Collection 类型包含一些私有字段,在这种情况下应该使用的字段是“m_KeyedNodesHash”字段,字段类型是 System.Collections.Generic.Dictionary(Of String , Microsoft.VisualBasic.Collection.Node),它包含一个名为“Keys”的属性,其中返回类型是 System.Collections.Generic.Dictionary(Of String, Microsoft.VisualBasic.Collection.Node).KeyCollection,并且是唯一的获取某个key的方法是将其转换为IEnumerable(Of String)类型,并调用ElementAt函数。
Private Function GetKey(ByVal col As Collection, ByVal index As Integer)
Dim listfield As FieldInfo = GetType(Collection).GetField("m_KeyedNodesHash", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim list As Object = listfield.GetValue(col)
Dim keylist As IEnumerable(Of String) = list.Keys
Dim key As String = keylist.ElementAt(index)
Return key
End Function