Inheritable Forms: Abstract

J

J.Marsch

I am trying to create a base form that all of our application's forms will
inherit from. I would like to mark it "abstract", but if I do, I get an
error from the form designer when I try to design forms that are
(non-abstract) children of the base form. The designer complains about not
being able to instance the base class (though I am designing the child
class). Now, I know that I could just use virtual methods and properties
instead of abstracts, and I could give my base form a private constructor to
keep people from instancing it directly.

However, I could have sworn that I came across some way of making the form
designer work with an abstract form. I thought that it had something to do
with placing an attribute in the child class, but I just can't seem to
remember what the attribute was (or if it's even real). Any hints?

-- Jeremy
 
J

Jay B. Harlow [MVP - Outlook]

Jeremy,
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
 

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