Reputation: 382
I am working on an example from a video at work that is supposed to add departments and employees to a
var d = new SortedDictionary<string, List<Employee>>();
but for some reason I cannot get it to compile with the following line of code.
d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));
I had it working for a Set but know the video had us add the class that is shown at the end of the Code example but I will list it here again.
public class EmployeeComparer : IEqualityComparer<Employee>
{
public EmployeeComparer() { }
public bool Equals(Employee x, Employee y)
{
return String.Equals(x.Name, y.Name);
}
public int GetHashCode(Employee obj)
{
return obj.Name.GetHashCode();
}
}
My question is what have I done wrong in these three lines of code so that I cannot compile and run my example:
1. d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));
2. d.Add("BB", new HashSet<Employee>(new EmployeeComparer()));
3. d.Add("CC", new HashSet<Employee>(new EmployeeComparer()));
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d)
{
d.Invoke("L");
}
static void Main(string[] args)
{
Program p = new Program();
//p.handler("Hello World");
//p.handler("DisneyLand");
//p.handler("Cattle Wars");
//p.testDel(p.handler);
var d = new SortedDictionary<string, List<Employee>>();
d.Add("AA", new HashSet<Employee>(new EmployeeComparer())); <-Error
d["AA"].Add(new Employee { Name = "A" });
d["AA"].Add(new Employee { Name = "B"} );
d["AA"].Add(new Employee { Name = "C"} );
d["AA"].Add(new Employee { Name = "C" });
d["AA"].Add(new Employee { Name = "C" });
d.Add("BB", new HashSet<Employee>(new EmployeeComparer())); <- Error
d["BB"].Add(new Employee { Name = "E"} );
d["BB"].Add(new Employee { Name = "F"} );
d["BB"].Add(new Employee { Name = "A"} );
d.Add("CC", new HashSet<Employee>(new EmployeeComparer())); <- Error
d["CC"].Add(new Employee { Name = "Z"} );
d["CC"].Add(new Employee { Name = "X"} );
d["CC"].Add(new Employee { Name = "Y"} );
foreach (var a in d)
{
Console.WriteLine(a.Key);
foreach (var e in a.Value)
{
Console.WriteLine("\t" + e.Name);
}
}
Console.ReadKey();
}
}
public class EmployeeComparer : IEqualityComparer<Employee>
{
public EmployeeComparer() { }
public bool Equals(Employee x, Employee y)
{
return String.Equals(x.Name, y.Name);
}
public int GetHashCode(Employee obj)
{
return obj.Name.GetHashCode();
}
}
}
Class Employee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Employee
{
public string Name { get; set; }
}
}
Error Table:
Error 1 Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.Equals(ConsoleApplication1.Employee, ConsoleApplication1.Employee)' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 66 21 ConsoleApplication1
Error 2 Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.Equals(ConsoleApplication1.Employee, ConsoleApplication1.Employee)' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 66 21 ConsoleApplication1
Error 3 Inconsistent accessibility: parameter type 'ConsoleApplication1.Employee' is less accessible than method 'ConsoleApplication1.EmployeeConmparer.GetHashCode(ConsoleApplication1.Employee)' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 71 20 ConsoleApplication1
Error 4 The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 34 13 ConsoleApplication1
Error 5 Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 34 25 ConsoleApplication1
Error 6 The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 41 13 ConsoleApplication1
Error 7 Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 41 25 ConsoleApplication1
Error 8 The best overloaded method match for 'System.Collections.Generic.SortedDictionary<string,System.Collections.Generic.List<ConsoleApplication1.Employee>>.Add(string, System.Collections.Generic.List<ConsoleApplication1.Employee>)' has some invalid arguments C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 46 13 ConsoleApplication1
Error 9 Argument 2: cannot convert from 'System.Collections.Generic.HashSet<ConsoleApplication1.Employee>' to 'System.Collections.Generic.List<ConsoleApplication1.Employee>' C:\Users\itpr13266\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 46 25
ConsoleApplication1
Working Code:
class Program
{
public delegate void Del(string e);
Del handler = DelegateMethod;
Dictionary<string, List<Employee>> d = new
Dictionary<string, List<Employee>>();
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
System.Console.ReadKey();
}
public void testDel(Del d)
{
d.Invoke("L");
}
static void Main(string[] args)
{
Program p = new Program();
//p.handler("Hello World");
//p.handler("DisneyLand");
//p.handler("Cattle Wars");
//p.testDel(p.handler);
var d = new SortedDictionary<string, HashSet<Employee>>();
d.Add("AA", new HashSet<Employee>(new EmployeeComparer()));
d["AA"].Add(new Employee { Name = "A" });
d["AA"].Add(new Employee { Name = "B"} );
d["AA"].Add(new Employee { Name = "C"} );
d["AA"].Add(new Employee { Name = "C" });
d["AA"].Add(new Employee { Name = "C" });
d.Add("BB", new HashSet<Employee>(new EmployeeComparer()));
d["BB"].Add(new Employee { Name = "E"} );
d["BB"].Add(new Employee { Name = "F"} );
d["BB"].Add(new Employee { Name = "A"} );
d.Add("CC", new HashSet<Employee>(new EmployeeComparer()));
d["CC"].Add(new Employee { Name = "Z"} );
d["CC"].Add(new Employee { Name = "X"} );
d["CC"].Add(new Employee { Name = "Y"} );
foreach (var a in d)
{
Console.WriteLine(a.Key);
foreach (var e in a.Value)
{
Console.WriteLine("\t" + e.Name);
}
}
Console.ReadKey();
}
}
Upvotes: 0
Views: 189
Reputation: 171178
Your dictionary values are typed as List<Employee>
and you are trying to add a HashSet<Employee>
value to it. It is as if you had written:
List<Employee> val = new HashSet<Employee>();
You wouldn't expect that to compile, either.
The error has nothing to do with the comparer you're using. I advise you to study compiler messages carefully and try to interpret them correctly. They usually tell you quite precisely what's wrong. In this case, they do!
Upvotes: 2