access pattern

J

Jeff.Boeker

Hello,

I have a group of custom controls and each control is derived from a
base class that performs some standard operations and enforces an
interface by having some abstract methods. Ideally, I don't want my
base class visible outside the assembly; also no instantiation, and no
access by nonderived types. Since C# does not allow multiple
inheritance, I derive my CBaseCtrl from System.Window.Forms.UserControl
and then derive each control from that, e.g. CButtonCtrl : CBaseCtrl.

My initial idea was to make the CBaseCtrl internal abstract and the
CButtonCtrl public but this results in a CS0060 inconsistent
accessibility error. Even if I change the base class to public
abstract so compilation succeeds, when I try to show the designer it
complains that the base class cannot be loaded (because it is
abstract). What is the best way out of this apparent conundrum?

Thanks,

Jeff
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Define a constructor in CBaseCtrl as private:

private CBaseCtrl(){}

This will prevent the creation of an instance of this class and you do not
have to mark it as abstract.

Ps:
You could declare it as protected if needed.
 
J

Jeff.Boeker

Hello Ignacio,

Thanks for the quick response. I should have mentioned that I had made
the constructor protected. However, I need to enforce an interface
with some abstract methods within CBaseCtrl so I still need to keep the
class abstract.

Thanks,

Jeff
 

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