0

I need to find a value inside a structure (the first that came in my mind was a static class, but anything will do) that has to be a hierarchical structure like:

public static class SheetGroups
{
    public static class Coverage {
        public static string sheet1 = "COVC1";
        public static string sheet2 = "COVC2";
    }

    public static class IncomeInvestment
    {
        public static string Income1 = "IEIC1";
        public static string Income2 = "IEIC2";
        public static string Income3 = "IEIC3";
        public static string Incomes4 = "IEIC4";
        public static string Investment1 = "IEIC5";
    }
}

The problem with this structure is that I need the groups (Coverage and IncomeInvestment) have values too (like an nested enum), and the other problem is that I have to implement a IfExistString method to find if a String exists within the values assigned.

I've been searching for a solution for a while now, but I cannot find a clean approach for this. For those who were wondering, I need this to validate the correct structure for a Excel file, the root (SheetGroups) stands for a zip file containing a number indetermined of Excel files, the nested clases (Coverage and IncomeInvestment) are Excel Files, then I have Sheets (COVC1, COVC2, etc), then I plan to have one more level, to have columns of tables in every sheet.

Thanks

Omar

culebrin
  • 125
  • 1
  • 15
  • Please clarify your business requirement: what exactly are you going to achieve with this nested static class? Rgds, – Alexander Bell Jun 27 '14 at 21:29
  • Thanks for your reply. One main task of my app is to validate cell by cell a set of Excel files zipped, that can have one or more sheets with one or more set of codified columns and rows (by codified I mean on the top row and first column I have codified values to identify cell content inside the sheet. There is a requirement to NOT to block/protect the contents of any meaningful value in the book (even the codified columns and rows) this being said, what I need to accomplish with this is to validate those codes (rows and cols) with a metadata embedded on the app. I hope this helps... – culebrin Jun 27 '14 at 23:22
  • btw It's a winforms stand-alone app with no internet or lan connection – culebrin Jun 27 '14 at 23:27

2 Answers2

3

I believe that this should be easier to handle if you use dictionary inside dictionary inside dictionary [...] aproach:

// I'm using collection initializers here! 
var groups = new Dictionary<string, Dictionary<string, string>>
{
     { 
          "Coverage", 
          new Dictionary<string, string> 
          {
               { "sheet1", "COVC1" },
               { "sheet2", "COVC2" }
               // And so on...
          }
     },
     {
          "IncomeInvestment",
          new Dictionary<string, string>
          {
               { "Income1", "IEIC1" }
               // And so on...
          }
     }
};

Now you can access values this way:

string sheet1 = groups["Coverage"]["sheet1"];

... or you might check if some key exists using out-of-the-box IDictionary<TKey, TValue>.ContainsKey(System.String) method!

I believe this approach should simplify your task and you can use standard, out-of-the-box dictionary capabilities to search, validate or access its values.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • @culebrin No problem. Think that your initial approach would require reflection and this ends in a complete overkill and over-engineering... – Matías Fidemraizer Jun 27 '14 at 23:29
  • Well, I tried, hard, but can't even begin to declare this code!, it keeps saying "invalid token '{' in class, struct, or interface member declaration". (the code: http://postimg.org/image/p0s8tgpyl/) – culebrin Jun 30 '14 at 15:13
  • @culebrin post in pastebin.com or something like that the whole code, or a portion of it so I can make you some suggestion – Matías Fidemraizer Jun 30 '14 at 15:17
  • ok, here it is: http://pastebin.com/Wf8sbSQr, the class its at the bottom of the file... I guess I will need some C# Dictionary basics guide book or something... – culebrin Jun 30 '14 at 15:33
  • 1
    @culebrin Uhm, I believe you misunderstood my approach. You need to create a class-level variable (a field) called (for example) `_dictionary` and in class constructor, set the whole dictionary of dictionaries to the whole variable. – Matías Fidemraizer Jun 30 '14 at 15:39
  • I didn't notice that a variable to assign, I solved it. It was in front of my eyes, I'll keep you posted... – culebrin Jun 30 '14 at 15:45
  • @culebrin No problem! Are you Spanish – Matías Fidemraizer Jun 30 '14 at 15:53
  • I'm peruvian, I could make it work, it looked like this: Dictionary>> _dict1 = new Dictionary>> { { "Cobertura", new Dictionary> { { "COBC1", new Dictionary { { "SER01", "Telf. Fija Abonado" }, { "SER02", "Telf. Publica" }, //... and so on... Thanks for your help, now I'll play with the search capabilities... – culebrin Jun 30 '14 at 17:31
  • @culebrin Nice! About search capabilities, it's about using LINQ! – Matías Fidemraizer Jun 30 '14 at 22:00
1

You can get the dictionary @matías mentioned by using reflection and LINQ like this:

var groups = typeof(SheetGroups).GetNestedTypes()
    .ToDictionary(k1 => k1.Name, v1 => v1
            .GetFields().ToDictionary(k2 => k2.Name, v2 => (string)v2.GetValue(null)));

groups variable will be of type Dictionary<string, Dictionary<string, string>>

thepirat000
  • 12,362
  • 4
  • 46
  • 72