Collection & List.

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Okay,

I'm inheriting from CollectionBase. I have an add method that calls List.Add as the examples suggest.

I'm not too bright here, but how in the world does "Add()" cause an "index out of bounds" error????!?!?!?!?!

I'm Adding? THe Index is never an issue.

If I were Inserting, then okay, but i'm not, so the index is determined as the last item in the list and voila.

Any ideas why this is happening?

THanks
Jaeden "Sifo Dyas" al'Raec Ruiner
 
Urgent: Why, oh why, oh why....

THis is the error i get upon executing
CollectionBase.List.Add()

And I don't get HOW it can even generate an IndexOutOfRangeException(). I mean, forget why it happens, i want to know HOW because this doesn't make any sense.

Exception System.IndexOutOfRangeException was thrown in debuggee:
Index was outside the bounds of the array.

System.Collections.IList.Add()
Add() - C:\Documents and Settings\skendrick\My Documents\SharpDevelop Projects\UniversalTool\PendingTasks.vb:100,4
Add() - C:\Documents and Settings\skendrick\My Documents\SharpDevelop Projects\UniversalTool\PendingTasks.vb:113,3

Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
 
If you're using .NET 2.0 as your profile says then you shouldn't be inheriting CollectionBase at all. You should be inheriting System.Collections.ObjectModel.Collection(Of T), where T is the type of the items the collection will store. For instance, to create a strongly-typed collection of Thing objects you declare the class like this:
VB.NET:
Public Class ThingCollection
    Inherits System.Collections.ObjectModel.Collection(Of Thing)
End Class
That's all you need. Unlike the CollectionBase class there's no need to declare any members at all if you only want standard functionality because it's ALL inherited from Collection(Of Thing). Only if you need to do extra processing when an item is added or the like do you need to add any code at all.
 
Back
Top