Solved

NameValueCollection

Posted on 2012-03-18
6
264 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

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.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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