3

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.

Community
  • 1
  • 1
Marcel
  • 15,039
  • 20
  • 92
  • 150

4 Answers4

2

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

Jarle Moe
  • 71
  • 1
  • ?? 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 May 05 '10 at 08:17
  • @Moe Could I have a virtual method in the base class that has an implementation?? Virtual methods can have implementations which can be overridden. Why did you choose an Abstract base if there were no abstract/virtual methods? Using a non-abstract base is just fine in this case. The only thing you lose is that you *could* create an instance of the base in this case; using ABC you would not be able to. – MPavlak May 17 '12 at 13:06
1

The best solution is here:

http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/

Using it now, it's elegant and gets around the underlying problem without breaking your nice OOP design.

Mel Padden
  • 983
  • 1
  • 9
  • 21
0

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!

Community
  • 1
  • 1
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
0

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
  • 5,108
  • 4
  • 30
  • 52