0

I have pre-defined several Dictionaries (with KeyPairValue elements) in a form's OnLoad event sub. I then added each dictionary to a collection that is defined outside the methods so that it is accessible between the form's subroutines. So far so good.

I would like to use this object to create String arrays to use as DataSources for several different ComboBoxes. In order to do this, I want to retrieve the key names that I previously assigned to the elements of the dictionaries within this collection. I would like to do this by looping through the elements of a specific dictionary within the collection and retrieving the key name. However, I am unable to figure out exactly how to retrieve the keyname. Here is what I am trying:

Collection.Item("Dictionary1")(counter).Key

What I am aiming to get at is, there is a collection of Dictionaries ("Dictionary1", "Dictionar2", etc.) which I can look-up by name. Once the dictionary is selected, I want to loop through the KeyValuePairs and retrieve the keynames. BUT, I also want to add each keyname as a member of a String array, so that I can assign that String array as the datasource for a comboBox.

Please let me know how I can do this without creating a counter outside of a For.. loop.

Thank you

4

3 回答 3

1

您应该能够按照以下方式做一些事情:

    Dim cValues As New System.Collections.Generic.Dictionary(Of String, Integer)
    Dim cKeys As New System.Collections.Generic.List(Of String)

    For Each sKey As String In cValues.Keys
        cKeys.Add(sKey)
    Next
于 2013-01-02T18:10:32.067 回答
0

尝试这个

Dim dict As Dictionary(Of String, Integer) = Collection.Item("Dictionary1")
Dim myArray As String()
Dim list As New List(Of String)

  For Each aKey As String In dict.Keys
        list.Add(aKey)
  Next

myArray = list.ToArray()
于 2013-01-02T18:59:31.940 回答
0

你应该能够做这样的事情:

  Dim DictCol As New List(Of Dictionary(Of T, T)) 'this is collection of Dictionaries as a list

  Dim Keyes As New List(Of T) 'will store keys in here

  For Each dict In DictCol

    For Each pair In dict

       Keyes.Add(pair.Key)

    Next

  Next

这段代码的问题是它不能识别每个键属于哪个字典。但是,如果您将 Keyes 保存在数组列表中,其中每个元素都有键和字典的名称,则可以解决该问题。

于 2015-03-19T08:39:25.660 回答