Tuesday, August 08, 2006

Visual Studio designer doesn't like generic controls

Generics rocks; coming from a C++ background it wasn't until .NET was blessed with generics (v2.0) that I began taking it seriously. Without at least some form of parametric polymorphism any programming language is simply hobbled.

Once the almighty Anders got around to giving us generics however, things were all rosy. I could safely (and speedily!) add and remove typed objects to my lists, write generic methods to perform operations and, generally, genericise the hell out of my code. Sure, it wasn't as powerful as C++'s templates but generics are easier to use and have their own advantages (side-stepping the code bloat issue and providing an easy way to package a generic library for example). Anyhoo, I'm using generics and I'm a happly lil' developer.

Until I try to derive from a WinForms control and create a generic class from it.

"Ohh noo, you can't do that!" complains the Visual Studio designer. "Of course I can" I reply (surely I'm not the only developer who talks to their computer?) as I scan the code for errors. There are none; the code compiles and even runs just fine. All I wanted was a type-safe CheckedListBox and generics are the perfect solution. Unfortunately the designer doesn't know how to deal with generic classes. If you create one, that's fine - but try to use it at design-time and you're in deep poo-poo. Bugger, it has a lot of great uses...

Anyone know any way around this limitation? And, for bonus points, do you know why that limitation is there (seems to me - with cursory thought - that there's no good reason for the limitation to exist)?


Anonymous said...

hi, I hope this will get fixed in the next version of VS. there are two workarounds I'm aware of.

1. put your control on the form in runtime. obviously you are about to loose all design-time advantages this way.
2. or wrap your generic class into non-generic one & use this one in designer.

I'm sure you already know this. just posting it for others.

Anonymous said...

I cannot believe it is 2009 and this still hasn't been fixed. What a pain in the botty!

Anonymous said...

2012... no generic controls in designer...