need help retrieving values from a Dictionary collection

brgdotnet
brgdotnet used Ask the Experts™
on
I am maintaining some VB.net code that uses key value pairs. Below is a simple example. What I need to know is how to access some of the values. See the example below.
Three values are added to the collection. So how can I iterate through the collection and retrieve the values "Canada", "Switzerland","Ireland"?

Dim objCollection As New Dictionary(Of String, String())
objCollection.Add("p1",{"Canada","int","INPUT","4"})

objCollection.Add("p1",{"Switzerland","int","INPUT","4"})

objCollection.Add("p1",{"Ireland","int","INPUT","4"})
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant
Commented:
You could try something like this.

objCollection.Add("p1", {"Canada", "int", "INPUT", "4"})
objCollection.Add("p2", {"Switzerland", "int", "INPUT", "4"})
objCollection.Add("p3", {"Ireland", "int", "INPUT", "4"})

For Each kvp As KeyValuePair(Of String, String()) In objCollection
	Console.WriteLine("{0} country is {1}", kvp.Key, kvp.Value(0))
Next kvp

Open in new window


P.S. I changed the keys to p1, p2, p3 as they need to be unique.
or
For Each key In objCollection.keys
	Console.WriteLine("{0} country is {1}",key, objCollection(key)(0))
Next kvp

Open in new window

Luis PérezSoftware Architect in .Net
Commented:
First of all, your written code will throw an exception because you're adding the key "p1" more than one time to the dictionary.

You can do something like this:
Dim objCollection As New Dictionary(Of String, String())
objCollection.Add("p1",{"Canada","int","INPUT","4"})
objCollection.Add("p2",{"Switzerland","int","INPUT","4"})
objCollection.Add("p3",{"Ireland","int","INPUT","4"}

'This For is to iterate thru the keys (p1, p2, p3)
For Each key As String In objCollection
    'objCollection(key) is a String array with the values associated to each key
    'The first Item in the array is the country
    Dim country As String = objCollection(key)(0)
    'The second value is the type ("int" or something else)
    Dim type As String = objCollection(key)(1)
    '... and so on
Next

Open in new window


Hope that helps.
Commented:
*NO POINTS*

Assuming that the order is the same; here is a proof of concept of the solutions posted by Norie and Ark:
Imports System

Module Program
	Sub Main(args As String())
		Dim objCollection As New Dictionary(Of String, String()) From
		{
			{"p1", {"Canada", "int", "INPUT", "4"}},
			{"p2", {"Switzerland", "int", "INPUT", "4"}},
			{"p3", {"Ireland", "int", "INPUT", "4"}}
		}

		Console.WriteLine("Nories solution:")
		For Each pair In objCollection
			Console.WriteLine($"{pair.Key} country is {pair.Value(0)}")
		Next
		Console.WriteLine()

		Console.WriteLine("Arks solution:")
		For Each key In objCollection.Keys
			Console.WriteLine($"{key} country is {objCollection(key)(0)}")
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.PNGLine 7 in Luis' implementation should be:
For Each key As String In objCollection.Keys

Open in new window

But demonstrates how to get the other values as well.

-saige-
brgdotnetcontractor

Author

Commented:
Awesome help from Awesome experts. Thanks so much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial