0

I need to write a method that will take two employee objects as input parameters and compare their ID's to determine whether or not they match.

It's incomplete at the moment; but so far, I have an Employee class that inherits first and last name properties from a "Person" class, and has ID as its own property. I'm writing the method in the employee file and have already instantiated 2 example employees in my program. As for overloading the ==, I'm running into an error that says "Employee" defines operator == but does not override Object.Equals." It also says I need to define "!=", but I'm confused on how to set up the != overload when it doesn't even figure into this method.

I've seen two ways of doing a compare method, one that returns true or false, and another that simply writes "match" to the console. Either of those would work for my purposes, but I can't figure out a workaround for the errors or how I'd change the code in this situation in order to determine a match between 2 employee ID's. Here is my code below; I'd appreciate any input on what may be going wrong! (I have a feeling it may be very off). I'm also unsure of how to call the method but I'm currently trying to figure it out.

Program file:

namespace OperatorOverload
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee example = new Employee();

            example.FirstName = "Kitty";
            example.LastName = "Katz";
            example.ID = 24923;

            Employee example2 = new Employee();
            example2.FirstName = "John";
            example2.LastName = "Dudinsky";
            example2.ID = 39292;

            Console.ReadLine();
        }
    }
}

Employee Class:

namespace OperatorOverload
{
    class Employee : Person
    {
        public int ID { get; set; }

        public static bool operator==(Employee employee, Employee employee2)
        {
            if (employee.ID == employee2.ID)

                return true;        
            else           
                return false;
            }
        }
    }

Person Class:

namespace OperatorOverload

{
    public class Person
    {
       public string FirstName { get; set; }
        public string LastName { get; set; }      
    }
}
FreddieMercury
  • 191
  • 1
  • 13
  • 3
    You might find your solution [here](https://stackoverflow.com/q/25461585/1219280) and [here](https://stackoverflow.com/q/10790370/1219280) – Veverke Oct 08 '18 at 07:07
  • 2
    Possible duplicate of [Operator overloading ==, !=, Equals](https://stackoverflow.com/questions/25461585/operator-overloading-equals) – Veverke Oct 08 '18 at 07:08
  • Yup, this is very similar to my situation. Thanks Veverke! – FreddieMercury Oct 08 '18 at 07:23
  • Quote "When a type overloads the equality operator, it must also override the Equals(Object) method to provide the same functionality. This is typically accomplished by writing the Equals(Object) method in terms of the overloaded equality operator, as in the following example. " – John Z. Li Oct 08 '18 at 08:21

5 Answers5

2

you need to also override the Equals method:

public override bool Equals(Object Obj)
{
    Person person = obj as Person;

    if(person == null)
        return false;

    return this.ID.Equals(person.ID);
}

Microsoft's recommandations:

Implement the GetHashCode method whenever you implement the Equals method. This keeps Equals and GetHashCode synchronized.

Override the Equals method whenever you implement the equality operator (==), and make them do the same thing.

mschuurmans
  • 1,088
  • 1
  • 12
  • 24
1

What You need to do is to use the Equals function and override it like this:

public override bool Equals(object obj)
{
    var item = obj as Employee;

    if (item == null)
    {
        return false;
    }

    return this.ID.Equals(item.ID);
}
kleinohad
  • 5,800
  • 2
  • 26
  • 34
1

The compiler is basically telling you that if you want to overload the == operator for the class Employee you will also have to override Object.Equals method and the != operator so that you will get a consistent semantic which you can use to compare instances of type Employee.

This means that you must not look for workarounds: you just have to overload the != operator and override Object.Equals so that Employee objects are compared by ID and not by reference (as they do by default if you don't provide your own equality semantic).

Enrico Massone
  • 6,464
  • 1
  • 28
  • 56
  • 1
    @FreddieMercury remember to also override Object.GetHashCode (which is a best practice each time you override Object.Equals so that you are able to use your class as a key inside a dictionary). That basic rule to do that is that **if two objects are equals than they must have the same hash code**. In your scenario this means that the hash code of an Employee object is calculated as **ID.GetHashCode()** – Enrico Massone Oct 08 '18 at 07:15
1

You should use this class. Override operator!=, operator== and Equals(object) method.

class Employee : Person
    {
        public int ID { get; set; }

        public static bool operator ==(Employee employee, Employee employee2)
        {
            if (employee.ID == employee2.ID)
                return true;
            else
                return false;

            //but you should use 
            //return employee.ID == employee2.ID;
        }

        public static bool operator !=(Employee employee, Employee employee2)
        {
            return employee.ID != employee2.ID;
        }

        public override bool Equals(object obj)
        {
            var emp = obj as Employee;
            if (emp == null)
                return false;

            return this.ID.Equals(emp.ID);
        }
    }
koviroli
  • 1,422
  • 1
  • 15
  • 27
1

This is a better way to override a Equals method:

public override bool Equals(object obj)
{
    if (obj is null) return false;
    if (ReferenceEquals(this, obj)) return true;
    if (obj.GetType() != this.GetType()) return false; //optional. depends on logic
    return this.ID.Equals(((Employee)obj).ID);
}
smolchanovsky
  • 1,775
  • 2
  • 15
  • 29