Solved

NameValueCollection

Posted on 2012-03-18
6
265 Views
Last Modified: 2012-06-22
Hi Experts,

I want to assign the following keys and values to a NameValueCollection:

Key "A" Values "1", "2", "3", "4"
Key "B" Values "5", "6", "7", "8"
Key "C" Values "9", "10", "11", "12"

How do I add and get these values?
0
Comment
Question by:DColin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37735778
Is there a reason you need NameValueCollection specifically? That collection is targeted for string keys and string values. What you have for your values appears to be an array of strings. Perhaps a Dictionary(Of String, String()) or Dictionary(Of String, List(Of String)) would suit your needs better.

Dictionary Examples
    Dim dict1 As New System.Collections.Generic.Dictionary(Of String, String())()

    dict1.Add("A", {"1", "2", "3", "4"})
    dict1.Add("B", {"5", "6", "7", "8"})
    dict1.Add("C", {"9", "10", "11", "12"})

    ' OR

    Dim dict2 As New System.Collections.Generic.Dictionary(Of String, List(Of String))()

    dict2.Add("A", New List(Of String)({"1", "2", "3", "4"}))
    dict2.Add("B", New List(Of String)({"5", "6", "7", "8"}))
    dict2.Add("C", New List(Of String)({"9", "10", "11", "12"}))

Open in new window

0
 
LVL 6

Expert Comment

by:Triskelion
ID: 37736010
Depending on how the data is arranged, you might be able to use an ILookup, which acts like the Dictionary(Of String, List(Of String))  mentioned by kaufmed.
Module Module1
   Sub Main()
      Dim lst_str = New List(Of String) From
      {
            "A,1,", "A,2", "A,3", "A,4",
            "B,5", "B,6", "B,7", "B,8",
            "C,9", "C,10,", "C,11,", "C,12"
      }

      Dim lkup_s2s As ILookup(Of String, String) =
      (
         From str In lst_str
         Let arr = str.Split(",")
         Select New With {.key = arr(0), .value = arr(1)}
      ).ToLookup(Function(k) k.key, Function(v) v.value)
   End Sub
End Module

Open in new window

0
 

Author Comment

by:DColin
ID: 37736657
If the values were stored as numbers would it make thing easier. I thought that a Namedvaluecollection was used when you have a single key and multiple values.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 6

Expert Comment

by:Triskelion
ID: 37737163
Well, there is a KeyValuePair(Of TKey, TValue),  that will store one of those groupings.
Dim kvp As New KeyValuePair(Of String, List(Of Integer))

Open in new window


will store the data as you expect for ("A": 1,2,3,4), for instance.
You would need another one for B and another one for C as you can only have one KEY in that object.

If you wanted them all in the same collection, you would need either a Dictionary(Of String, List(Of Integer))  or the ILookup(Of String, Integer).
With the latter two, you can have multiple KEYS in the collection.
0
 

Author Comment

by:DColin
ID: 37737314
I want to store a key which will always be a unique string and four double values associated with each key. What would you suggest I use and how would I add and retrieve the values?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37737409
If the values were stored as numbers would it make thing easier.
It's not so much the type of the data; rather it's the type of collection you are using.

I want to store a key which will always be a unique string and four double values associated with each key.
May I inquire as to how you are using them? I still maintain my original suggestion, but perhaps I don't understand your requirement.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question