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

  • Thread starter Thread starter dox
  • Start date Start date
D

dox

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
 
dox said:
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?
 
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.
 
dox said:
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.
 
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 !
 
dox said:
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.
 
Back
Top