Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary<int, List<string>>

I have something like this:

Dictionary<int, List<string>> fileList = new Dictionary<int, List<string>>();

and then, I fill it with some variables, for example:

fileList.Add(
    counter, 
    new List<string> { 
        OFD.SafeFileName, 
        OFD.FileName, 
        VERSION, NAME      , DATE  , 
        BOX    , SERIAL_NUM, SERIES,  
        POINT  , NOTE      , VARIANT
    }
);

Where counter is a variable that increment +1 each time something happens, List<string>{XXX} where XXX are string variables that holds some text.

My question is, how do I access these strings from the list, if counter == 1?

like image 512
DejmiJohn Avatar asked Jun 14 '13 12:06

DejmiJohn


3 Answers

You can access the data in the dictionary and lists just like normal. Remember, access a value in the dictionary first, which will return a list. Then, access the items in the list.

For example, you can index into the dictionary, which returns a list, and then index into the list:

         ------ Returns a list from the dictionary
         |  --- Returns an item from the list
         |  |
         v  v
fileList[0][0] // First item in the first list
fileList[1][0] // First item in the second list
fileList[1][1] // Second item in the second list
// etc.
like image 181
FishBasketGordo Avatar answered Oct 25 '22 07:10

FishBasketGordo


FishBasketGordo explains how you can access entries in your data structure. I will only add some thoughts here:

Dictionaries (based on hash tables) allow fast access to arbitrary keys. But your keys are given by a counter variable (counter = 0, 1, 2, 3, 4 ...). The fastest way to access such keys is to simply use the index of an array or of a list. Therefore I would just use a List<> instead of a Dictionary<,>.

Furthermore, your list seems not to list anonymous values but rather values having very specific and distinct meanings. I.e. a date is not the same as a name. In this case I would create a class that stores these values and that allows an individual access to individual values.

public class FileInformation 
{
    public string SafeFileName { get; set; }
    public string FileName { get; set; }
    public decimal Version { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    ...
}

Now you can create a list like this:

var fileList = new List<FileInformation>();
fileList.Add(
    new FileInformation {
        SafeFileName = "MyDocument.txt",
        FileName = "MyDocument.txt",
        Version = 1.2,
        ...
    }
}

And you can access the information like this

decimal version = fileList[5].Version;

If the keys don't start at zero, just subtract the starting value:

int firstKey = 100;
int requestedKey = 117;
decimal version = fileList[requestedKey - firstKey].Version;
like image 31
Olivier Jacot-Descombes Avatar answered Oct 25 '22 07:10

Olivier Jacot-Descombes


Dictionary uses Indexer to access its vallues via key.

List<string> items = fileList[counter];
var str0 = items[0];
var str1 = items[1];

Then you can do anything with the list.

like image 37
Mehmet Ataş Avatar answered Oct 25 '22 07:10

Mehmet Ataş