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();
}
}
}