views:

76

answers:

5

I have the following decleration:

private Dictionary<string, Dictionary<string, File>> listFiles = new Dictionary<string,Dictionary<string,File>>();

How do i add an item to the dictionary? Is there a better way to do something like this? Info: That stores a sourcefilename, destinationfilename, and the file itself.

Edit1: Just figured it out, all I want to store is 3 values, where the second object of the outer dictionary stores a dictionary object, which isn't really the best way to do it, seeing that it will always contain just one KeyValuePair.

Edit2: With File i meant the binary data.

Edit3: I have a unsorted file list, which i need to sort, and then send somewhere else.

+2  A: 

You can write a wrapper class Dictionary<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>>

Andrew Bezzub
A: 

To add a new File you'd do something like:

listFiles[srcFile] = new Dictionary<string, File>();
listFiles[srcFile][destFile] = file;

Note this will overwrite any mapping from an existing source file. However it seems what you really want is a map from (source) -> (dest, File), so in this case I'd make a class to contain the destination filename and the file and then create a dictionary to contain this lookup:

public class DestinationFileInfo { ... }

and then create a Dictionary<string, DestinationFileInfo>

Lee
+1  A: 

you can use

Dictionary<string, KeyValuePair<string, File>>

Hope this helps!!!

viky
this is not the same as dictionary of dictionaries, you woun't be able to have multiple secondary keys: it will be 1:1 correspondence as opposed to 1:N.
Al Bundy
I think he wants same behaviour!!
viky
A: 

I did this once in a class I called DoubleKeyDictionary. I needed the functionality of a dictionary object, but I had 2 keys. If you have the data locked down really tight, you might try just appending it, e.g. take the string from key #1, add a ~ or some delimiter, and then add string #2, like blue~large, which would give you unique keys. Again, this works better with strings, and requires you lock the data down so a ~ in your string doesn't fubar the whole thing.

So what I did was initially what you did: Dictionary>, but the syntax was a little clumsy. So although it doesn't answer your question explicitly, here's my solution:

Create a class called MultiKeyDictionary, it contains a DataTable internally, and has a method for retrieving your Customer object or whatever. That method takes a params object[], and uses the DataTable.Select method to get the object. That way you can have as many keys as you like. You'll need methods for adding objects of course.

LoveMeSomeCode
A: 

When you find yourself nesting one generic inside another, consider creating a class. For example:

// todo: give this a name that better describes its purpose
public class FileContainer
{
    string DestinationFileName { get; set; }
    File File { get; set; }
}

Now, instead of this:

Dictionary<string, KeyValuePair<string, File>>

you can work with this:

Dictionary<string, FileContainer>
Neil Whitaker