Inheritable Forms: Abstract

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J.Marsch

    #1

    Inheritable Forms: Abstract

    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


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Inheritable Forms: Abstract

    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 NotImplementedE xception, 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 NotImplementedE xception(); {
    #else
    public abstract void MyMethod();
    #endif

    Hope this helps
    Jay

    "J.Marsch" <jeremy@ctcdeve loper.com> wrote in message
    news:%23T54rMhU DHA.2248@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > 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[/color]
    not[color=blue]
    > 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[/color]
    to[color=blue]
    > 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[/color]
    do[color=blue]
    > 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
    >
    >[/color]


    Comment

    Working...