Visual Studio: create a UserControl class that comes from an abstract base class

I want to have an abstract base class for some of my custom UserControl . . The reason is obvious: they have common properties and methods (the main implementation of some interface elements in fact), and I want to implement them only once.

I did this by defining my abstract base class:

 public abstract class ViewBase : UserControl, ISomeInterface 

Then I decided to implement one of my views, as usual, using the constructor:

 public partial class SpecialView : UserControl //all OK 

So far, everything is fine. Now I am replacing the output of my SpecialView class SpecialView abstract base class:

 public partial class SpecialView : ViewBase //disrupts the designer 

Now the designer in Visual Studio 2008 no longer works by stating: The designer must create an instance of type 'ViewBase' but it cannot because the type is declared as abstract.

How can I get around this? I just don't want the same code to be copied for all of these views.

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

+3
source share
4 answers

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

+2
source

The best solution is here:

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

Using it now, it is elegant and circumvents the main problem without violating your good OOP design.

+1
source

It can be argued that it makes no sense from the point of view of design philosophy to expect that she can work with abstract control in the Designer. An abstract class tends to model the type of object for which, simply knowing that it does not adequately describe the โ€œXโ€, there is no such thing as an abstract bird or car, it is always a special type of bird or car. Considering it this way, if you want to view the user control in the designer, it should be a specific type of control, not abstract, otherwise what are you looking at? I see why this is annoying, but I can also understand why the constructor was encoded in this way.

0
source

Source: https://habr.com/ru/post/890189/


All Articles