1

I'm trying to test whether an object is equal to one in a list of objects given certain criteria (is name equal) and if it is, don't add it to list, otherwise add it. I have to use a method with this signature "static int Find(List c, Coffee x)". Find seeks x in c and returns a valid index (i.e., 0, 1, …) if x exists in c, returns -1 otherwise. My equals method doesn't seem to realize the names are the same when I pass exact matches. Why is this? Here's my code:

        Coffee obv = new Coffee();
        Decaf decafCoffee = null;
        Regular regularCoffee = null;
        List<Coffee> inventory = new List<Coffee>();

        if (some sxpression)
            {
                decafCoffee = new Decaf(name, D, C, M);
                find = obv.Find(inventory, decafCoffee);
                if (find == -1)
                {
                    inventory.Add(decafCoffee);
                }
            }


          public class Coffee : IDisposable
          {
              public override bool Equals(object obj)
              {
                  if (obj is Coffee)
                  {
                    bool isNameEqual = Name.Equals(this.Name);

                 return (isNameEqual);
                  }
        return false;
    }

        public int Find(List<Coffee> c, Coffee x)
    {
        if (c.Equals(x))
        {
            return 0;
        }

        return -1;
    }
        }          
3
  • 1
    List<Coffee> will never be equal to a Coffee object. Apr 29, 2013 at 17:17
  • 1
    How can a List<Coffee> ever equal a Coffee? Perhaps it's time to revisit your Find method? It's also a very bad idea to redefine equality without supplying a GetHashCode method that works on the same fields.
    – spender
    Apr 29, 2013 at 17:17
  • "Find seeks x in c .." Um, no it doesn't. It just checks to see if c.Equals(x). since "x" is type Coffee and "c" is type List<Coffee> they're not going to be equal. Apr 29, 2013 at 17:20

4 Answers 4

2

You are testing for equality on the List to an instance of Coffee. This will always return -1. What you want is c.Contains(x). Keep in mind when you override Equals you should also provide a similar override for GetHashCode(). Look here for Microsoft advice on implementing and overriding Equals on an object.

public int Find(List<Coffee> c, Coffee x) {
    return c.IndexOf(x);
}

public override int GetHashCode()
{
    return Name == null ? 0 : Name.GetHashCode();
}
0

Your error is here:

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Equals(x))  // <-- this will never return true
    {
        return 0;
    }

    return -1;
}

However, your Find method is unnecessary. Use List<T>.IndexOf to keep your concept:

var index = inventory.IndexOf(decafCoffee);
0

Your problem is here:

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Equals(x))
    {
        return 0;
    }

    return -1;
}

c is a List<Coffee> not a Coffee object.

You need to change your code so that it iterates over the list to see if it contains x:

for (int i = 0; i < c.Count; ++i)
    if (c[i].Equals(x))
        return i;

return -1
0

You can do as below, since you have Equals method you can use it for finding matching item

public int Find(List<Coffee> c, Coffee x)
{
    if (c.Any(i=>i.Equals(x))
    {
        return 0;
    }

    return -1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.