Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NameValueCollection

Posted on 2012-03-18
6
Medium Priority
?
270 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 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 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