1

I have a block of C# that looks like this

    public static string[,] FileTypesDict = new string[,] {
            {"txt", "ReadFile"},
            {"doc", "ReadMSOfficeWordFile"},
            {"docx", "ReadMSOfficeWordFile"},
            {"xls", "ReadMSOfficeExcelFile"},
            {"xlsx", "ReadMSOfficeExcelFile"},
            {"pdf", "ReadPDFFile"},
    };

I would like to use this inside another function like this...

    static void FindFiles() {
        foreach (string type[] in FileTypesDict) {
            // Find all files with extension
            string[] files = Directory.GetFiles(FilePath, type[0]);
            // To be used in another fun way, the longer description
            string filetype = type[1]
        }
    }

That, of course, doesn't work... It seems to only work when I do this -->

    static void FindFiles() {
        foreach (string type in FileTypesDict) {
            Console.WriteLine(type);
        }
    }

That will print each part of each array, like this -->

txt
ReadFile
doc
ReadMSOfficeWordFile
docx
ReadMSOfficeWordFile
xls
ReadMSOfficeExcelFile
xlsx
ReadMSOfficeExcelFile
pdf
ReadPDFFile

I'd basically like to look over the array of arrays and use each array inside of the loop...

3
  • 2
    Is there any reason you're not using a Dictionary<string,string>? Commented Mar 5, 2012 at 23:17
  • 2
    foreach (string[] type in FileTypesDict) - does that fix it? Commented Mar 5, 2012 at 23:18
  • Please don't prefix your titles with ".Net C#" and such. That's what the tags are for. Commented Mar 5, 2012 at 23:20

3 Answers 3

1

You should be using a Dictionary for this, as it makes things much neater and readable.

You could do this:

Dictionary<string, string> fileTypes = new Dictionary<string, string>()
{
    {"txt", "ReadFile"},
    {"doc", "ReadMSOfficeWordFile"},
    {"docx", "ReadMSOfficeWordFile"},
    {"xls", "ReadMSOfficeExcelFile"},
    {"xlsx", "ReadMSOfficeExcelFile"},
    {"pdf", "ReadPDFFile"}
};

foreach (KeyValuePair<string, string> file in fileTypes)
{
    Console.WriteLine(String.Format("{0} - {1}", file.Key, file.Value));
}

Or if you wanted to make the value a collection of string, you could do this:

   Dictionary<string, List<string>> fileTypesWithList = new Dictionary<string, List<string>>()
        {
            { "txt", new List<string>() { "Some", "Other", "Data" }}
        };

        foreach (KeyValuePair<string, List<string>> file in fileTypesWithList)
        {
            foreach (string s in file.Value)
            {
                Console.WriteLine(String.Format("{0} - {1}", file.Key, s));
            }

        }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the example and better solution to what I was trying!
Finally got a chance to try this today. It took me a bit to realize I needed 'using System.Collections.Generic;' but after that it seemed to work perfect. :D
1

The solution to your question is to define FileTypesDict as string[][]. Now I agree with the comments that the way you should do it is with a Dictionary.

1 Comment

Very simple resolution to what I was doing. I'll try to do the Dictionary instead since it's better. Thanks!
0

see here: http://msdn.microsoft.com/en-us/library/2s05feca.aspx

    static void FindFiles()
    {
        for (int i = 0; i < FileTypesDict.Length; i++)
        {
            var fileType = FileTypesDict[i, 0];
            var action = FileTypesDict[i, 1];

            string[] files = Directory.GetFiles(FilePath, fileType);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.