Solved

NameValueCollection

Posted on 2012-03-18
6
263 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

806 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