简体   繁体   中英

Why can I not call my function with the calling line of

Why can I not call my function like I have it now? The code for the printDictionary works perfectly if I have it in the main method. I want to move it to a function and then just use it that way with passing in my parameter but it give errors which are listed at the bottom.

Question: What have I missed that would cause me the errors below when I simply just want to call a function to print?

    printDictionary(d); <-- Error

Calling Code:

    printDictionary(d);

Function to Call:

    private void printDictionary(Dictionary<string, List<Employee>>
                                                            InputDictionaryParm1)
    {
        foreach (var a in InputDictionaryParm1)
        {
            Console.WriteLine(a.Key);
            foreach (var e in a.Value)
            {
                Console.WriteLine("\t" + e.Name);
            }
        }
        Console.ReadKey();
    }

Error Table:

Error   1   The best overloaded method match for 'ConsoleApplication1.Program.printDictionary(System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>)' has some invalid arguments  C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  52  13  ConsoleApplication1
Error   2   Argument 1: cannot convert from 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.SortedSet<ConsoleApplication1.Employee>>' to 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>'   C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs  52  29  ConsoleApplication1

From the looks of the error you are trying to pass a SortedDictionary into a method that accepts a Dictionary. These two types are not the same and can't be used interchangeably. They do both however implement IDictionary, so you could probably change the signature of the method to use the interface instead of a specific class.

You might consider changing the method signature to:

private void printDictionary(IDictionary<string, IEnumerable<Employee>>                                                             InputDictionaryParm1)
{
    foreach (var a in InputDictionaryParm1)
    {
        Console.WriteLine(a.Key);
        foreach (var e in a.Value)
        {
            Console.WriteLine("\t" + e.Name);
        }
    }
    Console.ReadKey();
}

I made two changes: switching from Dictionary to IDictionary and from List to IEnumerable. It doesn't appear you are using anything specific in List, so IEnumerable is a better choice (if all you need to do is enumerate over the values).

The error tells you what's going on:

cannot convert from 'SortedDictionary<string,SortedSet<Employee>>' to 'Dictionary<string,List<Employee>>'

In other words, you've declared d as a SortedDictionary<string,SortedSet<Employee>> but your method is expecting a Dictionary<string, List<Employee>> .

You'll have to change one of these types, as Dismissile's answer suggests, or convert between them. For example:

printDictionary(d.ToDictionary(p => p.Key, p => p.Value.ToList()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM