inheritance

G

Guest

I have a UserControl that I want to inherit from. Actually, I want to base a
lot of controls on it. It exposes a lot of overridable methods, some of
which I don't want to forget to override; these, I would LIKE to make
MustOverride, but when I do (and therefore make my UserControl MustInherit),
all of my descendant UserControls no longer have a design interface. How can
I make my ancestor UserControl MustInherit while still retaining the design
interface for its children?

Thanks,

Pat
 
H

Herfried K. Wagner [MVP]

pmcguire said:
I have a UserControl that I want to inherit from. Actually, I want to base
a
lot of controls on it. It exposes a lot of overridable methods, some of
which I don't want to forget to override; these, I would LIKE to make
MustOverride, but when I do (and therefore make my UserControl
MustInherit),
all of my descendant UserControls no longer have a design interface. How
can
I make my ancestor UserControl MustInherit while still retaining the
design
interface for its children?

You cannot. The designer will have to instantiate the base class in order
to show the designer.
 
J

Jay B. Harlow [MVP - Outlook]

Pat,
As Herfried states, you cannot as the Designer needs to instantiate the base
class.

What I do is to throw a NotImplementedException in each of the "must
override" methods of the base, this way at run time if they are not
overridden then an exception occurs.

Additionally you can wrap the method in an #if/#else and make the method
MustOverride in Release builds, while simply overridable in Debug builds.
Something like:

#if Debug Then
Public Class FormBase
Inherits System.Windows.Forms.Form
#else
Public MustInherit Class FormBase
Inherits System.Windows.Forms.Form
#end if

#if Debug Then
Public Overridable Sub Method1()
Throw New NotImplementedException
End Sub
#else
Public MustOverride Sub Method1()
#end if

End Class

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