MSDN - Collection(Of T).RemoveAt Method
-
viernes, 25 de mayo de 2012 4:53
The MSDN page for this method says this:
ArgumentOutOfRangeException
- index is less than zero.
- -or-
- index is equal to or greater than Count.
That doesn't make any sense, surely it should be "index is equal to or greater than (Count - 1)"
Since index is zero based.
I need to know since I'm Overriding the RemoveItem method for a Class that Inherits Collection(Of T)
Mike
Todas las respuestas
-
viernes, 25 de mayo de 2012 5:00
How do you Override it?It just depends on HOW YOU DO WITH THAT……
The source codes of Collection(T) seems fit to MSDN very well……
Please notice it that Any array defined in the .NET programming language seems starts from 0 as its index……
Public Sub RemoveAt(ByVal index As Integer) If (Me.items.IsReadOnly) Then ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection) End If If (index < 0 OrElse index >= Me.items.Count) Then ThrowHelper.ThrowArgumentOutOfRangeException() End If Me.RemoveItem(index) End Sub -
viernes, 25 de mayo de 2012 5:03
So what happens if you try to find the Collection Item at index -1, -2, -3...etc. ?
Will you be able to find it?
Obviously No.
So I see nothing wrong with the MSDN documentation.
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com -
viernes, 25 de mayo de 2012 5:21
I've no problem with the <0 check. that makes sense, that's why I specified the >= check.
So if a Collection has 5 elements then the index of the last element is 4.
That source code for RemoveAt would seem to permit Me.RemoveItem(5) which seems mad.
If you issued CollectionObject.RemoveAt(5) then it would result in an ArgumentOutOfRange exception.
Mike
-
viernes, 25 de mayo de 2012 5:24
Hey again:)
U should see that there are 5 elements——0,1,2,3,4——are their indexes……
And RemoveAt(5)——no index at all……:-)
-
viernes, 25 de mayo de 2012 5:35Moderador
The MSDN page for this method says this:
ArgumentOutOfRangeException
- index is less than zero.
- -or-
- index is equal to or greater than Count.
That doesn't make any sense, surely it should be "index is equal to or greater than (Count - 1)"
Since index is zero based.
I need to know since I'm Overriding the RemoveItem method for a Class that Inherits Collection(Of T)
Mike
Re-read it until you understand it... Since the indexes are zero based, ArgumentOutOfRangeException will occur if one of the following conditions exist:
Index is less than zero(there can never be a -1 index)
Index is equal to count(this means that you have not subtracted one to adjust for the zero index, therefore you are accessing a non-existent index)
Index is Greater than count(again, This means that you have attempted to access an index that does not exist)
So in conclusion, This makes perfect sense.
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
-
sábado, 26 de mayo de 2012 11:35
Index is equal to count (this means that you have not subtracted one to adjust for the zero index,
If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
Doh! (tx)
My fault, reading skills = nul point, though it was 6am!
Not sure the plain English campaign would approve of the wording and layout of the message, but hey ho!


