On Aug 7, 2012, at 2:39 AM, Steven Pack <steven_john_p...@yahoo.com.au> wrote:
> and deleting the item:
> 
> void DeleteItem(int position)
>         {
>             _log.Debug(Tag, "Deleting from position: " + position);
> 
>             var item = Headwords[position];
>             Headwords.Remove(item);
> 
>             RefreshListView();
>         }
> 
> 
> The DeleteItem method throws IndexOutOfRangeExceptions, firstly because it 
> gets called multiple times and also because the position index doesn't 
> actually represent the real index in the list somehow.

What is Headwords? What is Headword?

If I assume that Headwords is a List<Headword>, List<T>.Remove() [0] uses 
List<T>.IndexOf() which uses Array.IndexOf() [1] which uses 
EqualityComparer<T>.Equals(). If Headword doesn't implement 
IEquatable<Headword>, then EqualityComparer<Headword> will in turn use 
object.Equals(), which defaults to reference equality, which may or may not be 
correct.

Furthermore, if you have the same Headword instance in Headwords multiple 
times, List<T>.IndexOf() will return the _first_ index, which may not be the 
index you want.

In any event, you should probably  be using Headwords.RemoveAt(position), _not_ 
Headwords.Remove(Headwords[position]). The semantics are not equivalent between 
those expressions if/when the same element is present multiple times in the 
list.

 - Jon

[0] 
https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Collections.Generic/List.cs#L501
[1] 
https://github.com/mono/mono/blob/master/mcs/class/corlib/System/Array.cs#L2810

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to