Designer and abstract classes

J

Joe Vrba

I'm building a family of components derived from
UserControl. There's an abstract base class to ensure
basic functionality and then numerous other controls
derived from that.

The problem is that I'd like to manipulate some of the
derived controls using the VS.NET designer. However, when
the base class is made abstract, I get the error "The
designer must create an instance of type <...> but it
cannot because the type is declared as abstract."

Is there any way to allow the designer to instantiate the
abstract base class so I can work on the first tier
derived components visually? (Second tier components,
i.e. those derived from classes derived from the base
class work fine.)

Any suggestions would be appreciated.
 
J

Jay B. Harlow [MVP - Outlook]

Joe,
Is there any way to allow the designer to instantiate the
abstract base class so I can work on the first tier
derived components visually?
Sure, don't make them abstract! :-|

The forms designer needs to create an instance of the base class, as you are
able to set properties of the base class when you design the child class.

The general workaround is to have your base form abstract for release builds
of your app, but concrete for debug builds. This enables you to design the
form, during debug builds, then change to release builds and compile that
way.

What I do in the debug stub methods, is throw a NotImplementedException, as
a reminder that my derived classes need to implement the method.

Something like (untested):

#if DEBUG
public class FormBase : Form
{
#else
public abstract FormBase : Form
{
#endif

#if DEBUG
public virtual void MyMethod()
{ throw new NotImplementedException(); {
#else
public abstract void MyMethod();
#endif

Hope this helps
Jay
 
J

Jeffrey Tan[MSFT]

Hi Joe,

I do not know why you want to instantiate the abstract control in the
designer,
but the mechanism of the .net dose not allow you to do this.

However, I think you can put the abstract method of the abstract class into
an Interface,
Then your abstract base class can be concrete and can be instantiated.
Your other control can override the base class and the interface.

Hope this helps.

Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Joe Vrba" <[email protected]>
| Sender: "Joe Vrba" <[email protected]>
| Subject: Designer and abstract classes
| Date: Thu, 24 Jul 2003 13:04:50 -0700
| Lines: 18
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNSHtbcQZHd9lIIRbCFijTVTJp1PQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:171602
| NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I'm building a family of components derived from
| UserControl. There's an abstract base class to ensure
| basic functionality and then numerous other controls
| derived from that.
|
| The problem is that I'd like to manipulate some of the
| derived controls using the VS.NET designer. However, when
| the base class is made abstract, I get the error "The
| designer must create an instance of type <...> but it
| cannot because the type is declared as abstract."
|
| Is there any way to allow the designer to instantiate the
| abstract base class so I can work on the first tier
| derived components visually? (Second tier components,
| i.e. those derived from classes derived from the base
| class work fine.)
|
| Any suggestions would be appreciated.
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top