Richard Mitchell

Software Engineer - Red Gate Software
Engine programmer and factotum.

Type safe collections

Published Wednesday, November 23, 2005 9:04 AM

I must admit that creating a nice type safe collection for a new feature in one of our products took me a little time. There are some very good resources online on how to setup your own type safe collection so I thought I'd contribute a little to that resource.

The key to all this working nicely is to set up a good base class that has a thin implementation of the IList interface with one key addition...

protected virtual void OnValidate(object value)
{
if (!(value is ListObject))
{
throw new ArgumentException("ListObjects only in this List.");
}
}

This simple function in your base class is then used by pretty much all of the IList implementation functions that take an 'object' as the argument. Of course we want to make sure that this 'object' is one of the correct type - we do that by calling OnValidate() like...

public bool Contains(object value)
{
OnValidate(value);
return m_InnerList.Contains(value);
}

So if you don't pass an object of the correct type to one of the IList implementations then we throw an ArgumentException as one would expect. Now the cunning part is that we create this as part of a base class in our application and whenever we need a type safe collection we simply inherit from it and override the OnValidate() method.

protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is FunkyObject))
{
throw new ArgumentException("FunkyObjects only in this List.");
}
}

Then if we're being really really nice to our API users we can then go on and provide IList-like functions that take the appropriate type as an argument so they know what they're meant to be doing.

public bool Contains(InheritedObject funkyNewObject)
{
return base.Contains(funkyNewObject);
}

And there we have it, a simple way of producing a number of classes of type safe collections in .NET 1.1 C#. Of course this is what generics are for now although .NET 2 is still a little green.

Comments

No Comments
You need to sign in to comment on this blog

















<November 2005>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Larry Gonick: Geek of the Week
 Cartoonist, mathematician, historian and environmentalist. Larry Gonick proved that learning could be... Read more...

A SysAdmin's Guide to Change Management
 In the first in a series of monthly articles, ‘Confessions of a Sys Admin’, Matt describes the issues... Read more...

Exchange: Recovery Storage Groups
 It can happen at any time: You get a request, as Admin, from your company, to provide the contents of... Read more...

Build Your Own Virtualized Test Lab
 Desmond Lee explains the fundamentals of building a fully functional test lab for Windows Servers and... Read more...

Rendering Hierarchical Data with the Treeview
 It sometimes happens that Web Server controls that visualize data don't quite fit with the way that... Read more...