Solved

NameValueCollection

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

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now