1

I am new to vb.net. I want to create a key value pair or an array which ever is possible from a dynamic loop. The key name can have multiple entries I want output in following manner. I tried with dictionary but it do not allow duplicate keys.

In previous solutions which are in C#, I cannot find solution for my case since I want to get multiple values with the key like KeyName:{value1, value2, value3}. These three values must be within the KeyName not like KeyName: value1, KeyName: value2. I want to treat it like keyvalue pair.

Name1:{12, 12, 15}
Name1:{13, 12, 18}
Name2:{12, 11, 10}
Name3:{1, 2, 4}
Name4:{1, 2, 4}
Name1:{1, 2, 4}

Later on I need to print the keys with same name by grouping with the same key

ritesh khadka
  • 142
  • 1
  • 10
  • Possible duplicate of [Duplicate keys in .NET dictionaries?](https://stackoverflow.com/questions/146204/duplicate-keys-in-net-dictionaries) – muffi Feb 08 '18 at 05:56

1 Answers1

4

Alternative to a custom collection could be a List of KeyValuePairs :

Dim list = New List(Of KeyValuePair(Of String, Integer()))

list.Add(New KeyValuePair(Of String, Integer())("Name1", {1,2,3}))
list.Add(New KeyValuePair(Of String, Integer())("Name1", {1,2,3}))

For Each pair In list
    debug.print(pair.Key & ": " & String.Join(",", pair.Value))
Next

or Tuples :

Dim list = New List(Of Tuple(Of String, Integer()))

list.Add(Tuple.Create("Name1", {1,2,3}))
list.Add(Tuple.Create("Name1", {1,2,3}))

For Each pair In list
    Debug.Print(pair.Item1 & ": " & String.Join(",", pair.Item2))
Next
Slai
  • 19,980
  • 5
  • 38
  • 44
  • Tuple is nice for this... +1 – muffi Feb 08 '18 at 06:35
  • I think I need to split comma (,) later on to get those individual values. I had thought of this method to make it keyvalue pair. I was looking if there was any method rather than this. Let me try with this solution.. – ritesh khadka Feb 08 '18 at 06:42