Search code examples
c#ilistvirtualmodecollectionbase

How to tell when somebody is reading an item from a CollectionBase


I'm not sure if this is possible using a CollectionBase class. I'd like to know when somebody is accessing an item in a CollectionBase class.

The final goal is to create a "VirtualMode" (similar to the DataGridView control) that allows me to check and validate the data going out prior to the user getting it.

So what would happen is they could create a collection of say, 20 objects, internally we modify the IList to contain 20 null objects, then when they attempt to read an item, if it is null, we go to the external data source and read it in at that time. Then we replace the existing null object with the read class and next time they try to access it, they get the cached version.

After typing that out. I wonder if the OnValidate might be the right spot to do that.

Any assistance would be greatly appreciated.

Trevor Watson


Solution

  • It's not possible with a CollectionBase. OnValidate() is only called prior to OnInsert(),OnRemove(), and OnSet().

    You can inherit from ArrayList and override the indexer property (ArrayList.Item).