Dictionary <int, List <string>
I have something like this:
Dictionary<int, List<string>> fileList = new Dictionary<int, List<string>>(); and then I populate 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 increments +1 every time something happens, List<string>{XXX} , where XXX are string variables that contain some text.
My question is: how do I access these lines from a list if counter == 1?
You can access the data in the dictionary and lists as usual. Remember, first access the value in the dictionary that will return the list. Then access the items in the list.
For example, you can index a dictionary that returns a list and then indexes into a list:
------ Returns a list from the dictionary | --- Returns an item from the list | | vv 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. FishBasketGordo explains how you can access records in your data structure. I will just add some thoughts here:
Dictionaries (based on hash tables) allow you to quickly access arbitrary keys. But your keys are set by the counter variable (counter = 0, 1, 2, 3, 4 ...). The fastest way to access these keys is to simply use the index of the array or list. So I would just use List<> instead of Dictionary<,> .
Also, your list does not seem to contain anonymous values, but rather values โโthat have very specific and clear meanings. That is, the date does not match the name. In this case, I would create a class that stores these values โโand which allows 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 information like this
decimal version = fileList[5].Version; If the keys do not start from scratch, simply subtract the initial value:
int firstKey = 100; int requestedKey = 117; decimal version = fileList[requestedKey - firstKey].Version; Dictionary<int, List<string>> fileList = new Dictionary<int, List<string>>(); fileList.Add(101, new List<string> { "fijo", "Frigy" }); fileList.Add(102, new List<string> { "lijo", "liji" }); fileList.Add(103, new List<string> { "vimal", "vilma" }); for (int Key = 101; Key < 104; Key++) { for (int ListIndex = 0; ListIndex < fileList[Key].Count; ListIndex++) { Console.WriteLine(fileList[Key][ListIndex] as string); } } You can access the list via MyDic[Key][0] . When editing the list there will be no errors at runtime, however, this will lead to unnecessary values โโstored in the dictionary. That's better:
- assign
MyDict[Key]to the new list - edit the new list and then
- reassign the new list
MyDict[Key], and not edit a specific variable in the Dictionary with the list as values.
Code example:
List<string> lstr = new List<string(MyDict[Key]); lstr[0] = "new Values"; lstr[1] = "new Value 2"; MyDict[Key] = lstr;