1

I am working with c# in Xamarin and I've got an observable collection holding items in a list view. When I try to remove these items using .Remove() it returns false and the item remains in the list. I have implemented Equals() and GetHashCode() in the object and when I print its result I find that Equals is not being called by remove.

I know that the object references will not be the same between the item in the list and the one I am trying to remove, that is why I implemented an override for Equals()

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace CustomRenderer
{
    public class FishObject
    {
        public string Comment { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string Address { get; set; }
        public int NumberOfFish { get; set; }
        public DateTime createdAt { get; set; }
        public string ContactInfo { get; set; }
        public Image Picture { get; set; }
        public string Id { get; set; }

        public override bool Equals(Object obj)
        {
            FishObject fishObj = obj as FishObject;
            if (fishObj == null)
            {
                return false;
            }
            else
            {
                var number = this.NumberOfFish == fishObj.NumberOfFish;
                var coment = this.Comment == fishObj.Comment;
                var lat = this.Latitude == fishObj.Latitude;
                var lon = this.Longitude == fishObj.Longitude;
                var contact = this.ContactInfo == fishObj.ContactInfo;
                var result = number && coment && lat && lon && contact;
                System.Diagnostics.Debug.WriteLine("Equals res: " + result);
                return result;
            }
        }

        public override int GetHashCode()
        {
            return (this.Comment).GetHashCode();
        }

    }
}
Canyonman133
  • 49
  • 11
  • 1
    Then it sounds like your GetHashCode is wrong. Post the class. – MineR Jun 29 '18 at 05:34
  • 1
    I added the equals() and gethashcode() to the post – Canyonman133 Jun 29 '18 at 05:48
  • 2
    I looked at the source for `ObservableCollection.cs` and it doesn't do anything special, and just uses the standard `Collection.Remove()`. Testing also shows it should work: https://dotnetfiddle.net/FZyW0P – NPras Jun 29 '18 at 05:51
  • Is Comment a string? If so, are the comments identical? Need to see the remainder of the class. – MineR Jun 29 '18 at 05:52
  • You can also help rule out the hashcode problem by return 0 in your GetHashCode and seeing if it works then (or at least calls equals) – MineR Jun 29 '18 at 05:54
  • yea comment is a string, they should be identical because one is created off the other. – Canyonman133 Jun 29 '18 at 06:00
  • I tried returning 0 from hash code and equals was still not called – Canyonman133 Jun 29 '18 at 06:02
  • 2
    Then the problem must be elsewhere. Can you duplicate it in the dotnetfiddle that @NPras provided? – MineR Jun 29 '18 at 06:05
  • 2
    I tried a cutdown version of your code, and it still works fine: https://dotnetfiddle.net/EYSXJI Edit: yes, as @MineR said, a duplication of the problem in fiddle would be appreciated – NPras Jun 29 '18 at 06:07
  • 1
    Ooof the GetHashCode path was wrong from the beginning. ObservableCollection is basically a List, not a HashSet, so GetHashCode is entirely irrelevant. Therefore, your Equals should be called on every single item in the ObservableCollection (if Remove returns false). So it's most likely that your ObservableCollection is empty or does NOT contain FishObjects. – MineR Jun 29 '18 at 06:17
  • Yea you were absolutely right it was empty. – Canyonman133 Jun 29 '18 at 06:23

1 Answers1

0

Well, I'm dumb. it was a capitalization issue. Thanks for all your help, the fiddle really helped me find it.

Canyonman133
  • 49
  • 11