user2423014
user2423014

Reputation:

Dictionary and For loop issues in VB

how do I go about getting each value in a dictionary array if this is my dictionary .

        rebDictionary= New Dictionary(Of String, String())

    rebDictionary.Add("wrd", {"yap", "tap"})

i tried For Each rtbval As String In rebDictionary.value

but that does not work at all

Upvotes: 0

Views: 82

Answers (2)

Matt Wilko
Matt Wilko

Reputation: 27342

The following code iterates through all keys and values. You can select whichever parts you want/don't want:

    For Each kvp As KeyValuePair(Of String, String()) In rebDictionary
        Debug.WriteLine("Key:" + kvp.Key)
        For Each stringValue As String In kvp.Value
            Debug.WriteLine(" Value:" + stringValue)
        Next
    Next

You can just iterate through the keys:

    For Each key As String In rebDictionary.Keys
        Debug.WriteLine("Key:" + key)
    Next

Or through the values:

    For Each value As String() In rebDictionary.Values
        For Each stringValue As String In value
            Debug.WriteLine("value:" + stringValue)
        Next
    Next

But doing either of those probably isn't as useful as you don't know the corresponding key or value and I doubt that iterating through the key value pairs is likely to be much slower.

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

The values collection property is named Values, so try this:

For Each rtbval As String() In rebDictionary.Values

However, you're gonna iterate over a collection of String(), because your dictionary is Of(String, String()).

You can iterate over keys (which are String): rebDictionary.Keys or use LINQ SelectMany to iterate over flatten list of strings taken from you dictionary Values:

For Each rtbval as String In rebDictionary.Values.SelectMany(Function(x) x)

Upvotes: 3

Related Questions