473,404 Members | 2,187 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,404 software developers and data experts.

dictionary (hash table) with two values?

Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan

Apr 3 '07 #1
5 10197
ti***********@gmail.com <ti***********@gmail.comwrote:
Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).
Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 3 '07 #2


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
ti***********@gmail.com <ti***********@gmail.comwrote:
>Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Along these lines, you could also just store the values as an array...so
each key/value would be a key/array.

HTH,
Mythran
Apr 3 '07 #3
Mythran,

The only catch there is that if the two values are of different types,
then you have to have an array of object, which can have an overhead (if you
are boxing and unboxing value types).

In all cases, the solution that Jon suggested will provide a consistent,
expected behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mythran" <ki********@hotmail.comwrote in message
news:uO**************@TK2MSFTNGP04.phx.gbl...
>

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>ti***********@gmail.com <ti***********@gmail.comwrote:
>>Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Along these lines, you could also just store the values as an array...so
each key/value would be a key/array.

HTH,
Mythran


Apr 3 '07 #4
PS

<ti***********@gmail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...
Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan
As Jon suggested use an object with two properties. Here is a generic
version that you would use like:

DualValueDictionary<long, string, intd = new DualValueDictionary<long,
string, int>();
d.Add(123, "hello", 25);
if(d[123].Value1 == "hello") .....
public class DualValueDictionary<Tkey, V1, V2: Dictionary<Tkey,
DualValues<V1, V2>>
{
public void Add(Tkey key, V1 value1, V2 value2)
{
base.Add(key, new DualValues<V1,V2>(value1, value2));
}
}
public class DualValues<V1, V2>
{
public DualValues(V1 v1, V2 v2)
{
this.value1 = v1;
this.value2 = v2;
}
private V1 value1;
public V1 Value1
{
get
{
return this.value1;
}
}
private V2 value2;
public V2 Value2
{
get
{
return this.value2;
}
}
}

PS
Apr 3 '07 #5
Jon, simple elegant solution. Thanks.

PS, thanks for the code. You just guided me around a few pitfalls I
would have got stuck on (I would not have thought of generic classes,
as I'm new to them).

Thanks all,
Titan

Apr 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tony | last post by:
Hello. What is hash table? i could only find small reffrence to hash table .. but what exactly is this and how can i creat them? Is there any website with tutorial on this subject ? Thanks
4
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
2
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table...
24
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
2
by: gurmeet07 | last post by:
hey guys i m new to .net and facing one problem on click of a button (saving the data to the hash table) like this : there are 7 textboxes and first textbox1.text is the key for hash table n...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
5
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi.. I wrote following code. iindex = 0; foreach (XmlNode Labelnodes1 in Labelnode) { countlab = Labelnodes1.Attributes.Count; for (int i = 0; i < countlab; i++) {
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.