0

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

1

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Tuple is nice for this... +1
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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.