views:

268

answers:

3

Hi All,

I want to have an abstract base class for some of my custom UserControl's. The reason is obvious: they share some common properties and methods (a basic implementation of some elements of an interface actually), and I want to implement them only once.

I have done this by defining my abstract base class:

public abstract class ViewBase : UserControl, ISomeInterface

Then I went to implement one of my views, as usual, with the designer:

public partial class SpecialView : UserControl //all OK

Up to here all is fine. Now I replace the derivation of my SpecialView class with the abstract base class:

public partial class SpecialView : ViewBase //disrupts the designer

Now, the designer in Visual Studio 2008 won't work anymore, stating: The designer must create an instance of type 'ViewBase' but it cannot because the type is declared as abstract.

How can I circumvent this? I just do not want to have the same code copied for all those views.

Info: there is a question question with virtual methods, instead of abstract classes, but there is no suitable solution for me.

+1  A: 

Instead of using abstract class, you can mark the functions virtual and override them in the inheriting classes

Jarle Moe
?? Actually, I do not want to override, I want to have an implementation that I can inherit. Could I have a virtual method in the base class that has an implementation??
Marcel
In the end, I have done like proposed.
Marcel
A: 

Try this solution from Urban Potato, which worked for me, with a strange side effect that I never really had explained, and never got a good workaround. Maybe you'll get lucky and won't have that side-effect!

Shaul
A: 

One could argue that it doesn't make sense in terms of design philosophy to expect to be able to work with an abstract control in the Designer. An abstract class tends to model a type of object for which simply knowing that it's an 'X' doesn't adequately describe it - there's no such thing as an abstract Bird or Car, it's always a specific type of bird or car. Looking at it this way, if you want to view a custom control in the designer, it has to be a specific type of control rather than an abstract one, otherwise what are you looking at? I can see why it's annoying, but I can also see why the Designer was coded in this way.

Tom W