Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

DataBinding interface?

In my application there are a number of "typesafe enums" which is to say classes with private constructors that make available the only instances of the class available.  For example you could have HotDrink which makes available HotDrink.Coffee, HotDrink.Tea and HotDrink.Chocolate and it is not possible to have any other instances of HotDrink.  Thus you can do == comparisons like:

if (x == HotDrink.Coffee) {


OK, so I have lots of these typesafe enum classes because I think they're great (the idea comes from Joshua Bloch's Effective Java - a really good book).  And inevitably in my ASP.NET application there are lots of places where I want to use them in databinding.

I make an array of all the instances available from each class called Values.  Then there's a lot of code in my app that goes:

dropdown.DataSource = HotDrink.Values;
dropdown.DataValueField = "Code";
dropdown.DataTextField = "Name";

So I thought maybe I would create an interface IDataBindable that each of these classes could implement that takes a ListControl (might run into trouble with what class the parameter is because of the crap hierarchy in .NET form controls but that's another posting) and does that (and inserts a blank option at the top that says "[Select Hot Drink]").

Then I thought the code was so similar I could create an abstract class that does it reading DataValueField, DataTextField and the value/text of the first option from private methods that are abstract in the abstract class.

Then I thought, "hang on, surely this is inthe class library?"  But I can't find anything. 

What's the standard way to declare that a class can be used in DataBinding or are they so concerned with databases that there is no way for a non-Database related class to declare it?  Or am I completely mental and all my ideas are wrong?  I need to know!

Thomas David Baker
Thursday, November 06, 2003

If I understand your needs correctly, I believe that you're looking to implement SystemComponentModel.IBindingList.

Greg Hurlman (
Friday, November 07, 2003

*  Recent Topics

*  Fog Creek Home