如何从集合中获取键和值 VB.Net [英] How to get the Key and Value from a Collection VB.Net

查看:27
本文介绍了如何从集合中获取键和值 VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在遍历 vb.net 集合时如何从它获取键值?

How do you get the key value from a vb.net collection when iterating through it?

    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

推荐答案

很确定你不能直接从 Microsoft.VisualBasic.Collection.

Pretty sure you can't from a straight Microsoft.VisualBasic.Collection.

对于上面的示例代码,请考虑使用 系统.Collections.Specialized.StringDictionary.如果这样做,请注意 Add 方法的参数与 VB 集合相反 - 先是键,然后是值.

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.

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

这篇关于如何从集合中获取键和值 VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆