1ftw1
1ftw1

Reputation: 666

class inheritance cant get my Equals() to work

I have a class:

  class Stock : Product
  {
  }

And in that class i have made a Equals method:

  public bool Equals(Product p)
  {
      return (p.Id == this.Id);
  }

But it's not working. It tells me that Equals:

Warning 1 'WindowsFormsApplication1.Stock.Equals(WindowsFormsApplication1.Product)' hides inherited member 'WindowsFormsApplication1.Product.Equals(WindowsFormsApplication1.Product)'. Use the new keyword if hiding was intended.

C:\Users\tom\Desktop\uni\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1\Stock.cs    36  21  WindowsFormsApplication1

Anyone know why this is?

Upvotes: 0

Views: 1119

Answers (5)

Amiram Korach
Amiram Korach

Reputation: 13296

Equals in inherited from System.Object. All objects inherit this class.

public override bool Equals(object p)
{    
    return ((Stock)p.Id == this.Id);
}

EDIT: In order to override, the parameter needs to be of the same type. In object it is of type object. If you declared this method in Product like:

public bool Equals(Product p)
{    
    return (p.Id == this.Id);
}

then you have to change the parameter type in Stock.Equals to Product. However, this Equals method is hiding object.Equals and I think this is not a good design. If you want to use the Equals method right, you need to override because many components call this method when they search in a collection and they will call object.Equals anyway and won't use your code if you use hiding and not overriding. If those methods are something internal in your project and not related to object.Equals mechanism, choose a different name for this method to avoid confusion.

Upvotes: 5

Habib
Habib

Reputation: 223332

It is just a warning, it will work as expected. Since your product class defines Equals, the child class Stock is defining its own Equals method.

Assuming you have the following class setup:

class Product
{
    public bool Equals(Product p)
    {
        return true;
    }
}

and

  class Stock : Product
    {
        public bool Equals(Product p)
        {
            return true;
        }
    }

Here you will get the warning:

Stock.Equals(Product)' hides inherited member 'Product.Equals(Product)'. Use the new keyword if hiding was intended.

You can simply use the new keyword to tell the compiler that Yes the hiding is intended. So you method inside stock class would be would be:

new public bool Equals(Product p)
{
    return true;
}

You may wanna see new modifier - msdn

Upvotes: 0

Klamore74
Klamore74

Reputation: 578

You need to put override to implict declare the replacement of the parent method:

public override bool Equals(Product p)     
{          
   return (p.Id == this.Id);      
} 

This warning help the coder to avoid unwanted override of parent method.

Upvotes: 0

Lukazoid
Lukazoid

Reputation: 19426

It seems like you have an bool Equals(Product) method already defined on Product, make it virtual and override it in Stock.

Upvotes: 0

Chepene
Chepene

Reputation: 1128

You need to override Equals method, which is already used in Product Class.

public override bool Equals(Product p)
{
    return (p.Id == this.Id);
}

Or use new keyword. But I don't think 'new' is what you want.

Upvotes: 7

Related Questions