Advertisement

07.20.2008 at 10:01PM PDT, ID: 23580938
[x]
Attachment Details

IEqualityComparer - GetHashCode

Asked by Thomasian in Microsoft Visual Basic.Net, .Net Editors & IDEs, Visual Studio 2008

Tags: VB 2008

I am trying to create an IEqualityComparer class which compares 2 fields of a class. The code below works but I'm not sure what to put on the GetHashCode function. I found that it is not using the function when I used the Contains function of the List collection. What exactly is the use of the GetHashCode function?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Module Module1
 
    Sub Main()
        Dim EmployeeList As New List(Of Employee)
 
        Dim Employee1 As New Employee
        Dim Employee2 As New Employee
 
        Employee1.ID = 1
        Employee1.Name = "James"
        Employee2.ID = 2
        Employee2.Name = "james"
 
        EmployeeList.Add(Employee1)
 
        Console.WriteLine(EmployeeList.Contains(Employee2, New Employee.EqualityEmployee))
 
        Console.ReadKey()
    End Sub
 
    Public Class Employee
        Public ID As Int16
        Public Name As String
 
        Public Class EqualityEmployee
            Implements IEqualityComparer(Of Employee)
 
            Public Function Equals1(ByVal x As Employee, ByVal y As Employee) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Employee).Equals
                Return x.ID.Equals(y.ID) OrElse String.Equals(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase)
            End Function
 
            Public Function GetHashCode1(ByVal obj As Employee) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Employee).GetHashCode
                Return obj.GetHashCode
            End Function
        End Class
    End Class
 
End Module
 
Loading Advertisement...
 
[+][-]07.21.2008 at 08:37PM PDT, ID: 22056284

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.21.2008 at 09:02PM PDT, ID: 22056374

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 10:09AM PDT, ID: 22061439

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 06:48PM PDT, ID: 22065398

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.22.2008 at 09:12PM PDT, ID: 22065922

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, .Net Editors & IDEs, Visual Studio 2008
Tags: VB 2008
Sign Up Now!
Solution Provided By: FernandoSoto
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906