473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how can I create a hash of hash (or key with multiple values)

Hi,

In perl I can create a hash of hash which would have something like

$MYHASH{'USER1'}{'SPORT'}{'FOOTBALL'} =1

$MYHASH{'USER1'}{'SPORT'}{'TENNIS'} =1

http://docstore.mik.ua/orelly/perl/prog3/ch09_04.htm shows a type of example on the perl page.

This way I can loop through the users and the sports and find out what sports they are associated to.

How can I create a similar stucture in c Sharp, I have been looking at hashtables but they only show my key <=> values. Where I need to have a key with an array of values that I can index in on.

Can anyone help here please or point me to some examples ?

Thanks in advance.
Mar 11 '09 #1
3 1871
PRR
750 Expert 512MB
If you are looking for collection that allows multiple values per key then you can go in for NameValueCollection
Expand|Select|Wrap|Line Numbers
  1. NameValueCollection mycol = new NameValueCollection();
  2.             mycol.Add("1", "1");
  3.             mycol.Add("1", "2");
  4.             mycol.Add("1", "3");
  5.             mycol.Add("1", "4");
  6.  
  7.             foreach (string s in mycol.GetValues(0))
  8.             {
  9.                 Console.WriteLine(mycol[s]);
  10.             }
  11.  
Mar 11 '09 #2
Thanks this is great, I noticed on the link that NameValueCollection is for 2008 only is there an eqilavant way of doing this for VS 2005 ?
Mar 11 '09 #3
vekipeki
229 Expert 100+
It exists in VS2005 also: NameValueCollection.

But Hashtable also implements IEnumerable for DictionaryEntry objects, so you can also write:

Expand|Select|Wrap|Line Numbers
  1. foreach (DictionaryEntry entry in myHashtable)
  2. {
  3.    Console.WriteLine("Key = {0}, Value = {1}", entry.Key, entry.Value);
  4. }
There is also a generic Dictionary<TKey,TValue> you can use, which implements IEnumerable for KeyValuePair<TKey,TValue> items.
Mar 11 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
9
by: Karl O. Pinc | last post by:
I want to return multiple values, but not a set, only a single row, from a plpgsql function and I can't seem to get it to work. (I suppose I'd be happy to return a set, but I can't seem to make...
1
by: method | last post by:
Hi all i am usiing this code to write mulitiple values to registery but it only write one value. could any one help me make this code so that it write multiple values to registery. Currently it only...
1
by: chariclark | last post by:
This may be a quick fix post... ---------------------------- I am having trouble passing multiple values into stored procedure. Here it is below: CREATE Procedure spGetAssociateds ( @PDSI...
4
by: roshnair | last post by:
Hi i have a small problem . I need to change one query to stored procedure and call it in my vb.net application.Query is : Select * from emp where empid in (‘001’,’002’) For this query I...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
2
by: Zeroth0 | last post by:
I would like to create a database of music. An album has many values for the fields: mood, style etc. How would I structure my tables for this situation? Is it possible to have a field with many...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.