PC Review


Reply
Thread Tools Rate Thread

Dictionary<TKey,TValue> as a Dictionary key in C# 2.0

 
 
dox
Guest
Posts: n/a
 
      19th Mar 2008
Hello,

I'm having trouble using Dictionary classe. I want to use a Dictionary as a
key for another Dictionary. The following code gives a sample of what I'm
talking about.

Dictionary myDates = new Dictionary<Dictionary<string, string>, DateTime>();

The 'Add' or 'ContainsKey' methods need a Dictionary<string, string> as a
(first) parameter, but how to pass this kind of parameter without
instanciating another Dictionary<string, string> variable.

Any suggestion ?
Thanks
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Mar 2008
dox <(E-Mail Removed)> wrote:
> I'm having trouble using Dictionary classe. I want to use a Dictionary as a
> key for another Dictionary. The following code gives a sample of what I'm
> talking about.
>
> Dictionary myDates = new Dictionary<Dictionary<string, string>, DateTime>();
>
> The 'Add' or 'ContainsKey' methods need a Dictionary<string, string> as a
> (first) parameter, but how to pass this kind of parameter without
> instanciating another Dictionary<string, string> variable.


What would you want to pass in as the parameter? What kind of key are
you wanting to add/test?

To be honest, Dictionary<TKey,TValue> makes a fairly unlikely hash key
- it doesn't override Equals or GetHashCode, so you have no equality
beyond identity.

Could you give more details about what you're trying to actually
achieve? What's the end goal?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
dox
Guest
Posts: n/a
 
      19th Mar 2008
Here is my problem :
I have several server (identified by their names 'Server1', 'Server2'...)
and several message levels ('Information', 'Warning', 'Error'...).
I want for each couple 'server/message level' store the date of the last
message sent. I thougth about a structure like this :

"Server1"/"Information" -> 2008/03/12 14:00:00
"Server1"/"Error" -> 2008/03/11 09:55:00
"Server2"/"Error" -> 2008/02/27 10:11:00
....

In this structure, the key would be a Dictionary<string, string> identifying
the server and the message level. The value would be the DateTime. I know I
could achieve it in an easier way by concatenating the key
("Server1:Information") and using a Dictionary<string, DateTime>. I'm just
wondering if it is possible to use a Dictionary as a key, and how to use it.


"Jon Skeet [C# MVP]" wrote:

> dox <(E-Mail Removed)> wrote:
> > I'm having trouble using Dictionary classe. I want to use a Dictionary as a
> > key for another Dictionary. The following code gives a sample of what I'm
> > talking about.
> >
> > Dictionary myDates = new Dictionary<Dictionary<string, string>, DateTime>();
> >
> > The 'Add' or 'ContainsKey' methods need a Dictionary<string, string> as a
> > (first) parameter, but how to pass this kind of parameter without
> > instanciating another Dictionary<string, string> variable.

>
> What would you want to pass in as the parameter? What kind of key are
> you wanting to add/test?
>
> To be honest, Dictionary<TKey,TValue> makes a fairly unlikely hash key
> - it doesn't override Equals or GetHashCode, so you have no equality
> beyond identity.
>
> Could you give more details about what you're trying to actually
> achieve? What's the end goal?
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Mar 2008
dox <(E-Mail Removed)> wrote:
> Here is my problem :
> I have several server (identified by their names 'Server1', 'Server2'...)
> and several message levels ('Information', 'Warning', 'Error'...).
> I want for each couple 'server/message level' store the date of the last
> message sent. I thougth about a structure like this :
>
> "Server1"/"Information" -> 2008/03/12 14:00:00
> "Server1"/"Error" -> 2008/03/11 09:55:00
> "Server2"/"Error" -> 2008/02/27 10:11:00
> ...
>
> In this structure, the key would be a Dictionary<string, string> identifying
> the server and the message level. The value would be the DateTime. I know I
> could achieve it in an easier way by concatenating the key
> ("Server1:Information") and using a Dictionary<string, DateTime>. I'm just
> wondering if it is possible to use a Dictionary as a key, and how to use it.


You still wouldn't use a Dictionary as a key here. You'd use a
dictionary as the *value* of the outermost Dictionary:

Dictionary<string,Dictionary<string,DateTime>> foo;

Then you could do: Console.WriteLine(foo["Server1"]["Information"]).

However, you've already actually given the crux of a much better
solution, without realising it:

"I want for each couple 'server/message level'"

So, you want a type which encapsulates both the server and the message
level, and defines equality and hashcode based on that information
pair.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
dox
Guest
Posts: n/a
 
      19th Mar 2008
You're right. After all, Dictionary as a key is probably not the best solution.
Let's write a custom Type, encapsulating both the server and message level.

Thanks a lot !

"Jon Skeet [C# MVP]" wrote:

> You still wouldn't use a Dictionary as a key here. You'd use a
> dictionary as the *value* of the outermost Dictionary:
>
> Dictionary<string,Dictionary<string,DateTime>> foo;
>
> Then you could do: Console.WriteLine(foo["Server1"]["Information"]).
>
> However, you've already actually given the crux of a much better
> solution, without realising it:
>
> "I want for each couple 'server/message level'"
>
> So, you want a type which encapsulates both the server and the message
> level, and defines equality and hashcode based on that information
> pair.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      19th Mar 2008
dox <(E-Mail Removed)> wrote:
> You're right. After all, Dictionary as a key is probably not the best solution.


Not just that - it's not really a feasible one. Did you see what I
meant about using a dictionary as the first level *value* rather than
key?

> Let's write a custom Type, encapsulating both the server and message level.


That would indeed be the best approach, IMO. Don't forget to override
both Equals and GetHashCode, in a consistent manner.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialize a Dictionary(Of TKey, Of TValue) Art Microsoft VB .NET 1 7th Jul 2009 04:25 PM
Dictionary<TKey, TValue> with user-defined class implementingIEquatable<T> as key craig.wenger@gmail.com Microsoft Dot NET Framework 1 1st Feb 2008 02:18 PM
Using Dictionary<TKey,TValue> in .net compact framework 2.0 Daniel.Kristensen@gmail.com Microsoft Dot NET Compact Framework 2 11th Oct 2006 08:13 PM
threading: acessing a Dictionary<TKey,TValue> with foreach bonk Microsoft C# .NET 7 10th Aug 2006 07:32 AM
How Dictionary<TKey,TValue> is checking keys? Shimon Sim Microsoft C# .NET 2 13th Jun 2006 10:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:09 AM.