why member inaccessible?

M

Marty McDonald

public class BaseDALC
{
//Don't allow instantiation
private BaseDALC() // To compile properly, I had to change this to
"protected"
{ }
}
....
public class StatisticsDALC : BaseDALC
{
}

error CS0122: 'AirportInformation.DAL.BaseDALC.BaseDALC()' is inaccessible
due to its protection level. Why the compile problem? Does inheritance
require accessibility to the base class constructor? Thanks! --Marty
 
S

Syanide

you cannot inherit a class with a private constructor.. as the inheriting
class cannot call any of its methods..
 
M

Marty McDonald

Thanks! Is that true even if the base class will have nothing but static
members?
 
M

mikeb

Marty said:
public class BaseDALC
{
//Don't allow instantiation
private BaseDALC() // To compile properly, I had to change this to
"protected"
{ }
}
...
public class StatisticsDALC : BaseDALC
{
}

error CS0122: 'AirportInformation.DAL.BaseDALC.BaseDALC()' is inaccessible
due to its protection level. Why the compile problem? Does inheritance
require accessibility to the base class constructor? Thanks! --Marty

Since you didn't specify a constructor for StatisticsDALC, the compiler
provides a default constructor of the form:

public StatisticsDALC(): base() {}

(See MSDN C# Specification section 10.10.4)

So the constructor for the base class must be accessible.

Note that defining a constructor explicitly does not get around this -
some constructor in the base class needs to be called, however if you
explicitly define a constructor for your class, you can control which
constructor gets called (which still needs to be accessible, and
therefore cannot be private).

See the MSDN C# Spec 10.10.1 for details.
 
J

Jeffrey Tan[MSFT]

Hi Marty,

Thank you for posting in the community! My name is Jeffrey, and I will be
assisting you on this issue.

=====================================================
I agree with mikeb's reply.

If a class contains no instance constructor declarations, a default
instance constructor is automatically provided. That default constructor
simply invokes the parameterless constructor of the direct base class.
Because your DIRECT base class constructor is inaccessible, If the direct
base class does not have an accessible parameterless instance constructor,
a compile-time error occurs.

More detailed information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_10_10_4.asp

Also, your inherited class's constructor will invoke the direct base
class's constructor. All instance constructors (except those for class
object) implicitly include an invocation of another instance constructor
immediately before the constructor-body.
An instance constructor initializer of the form base(argument-listopt)
causes an instance constructor from the direct base class to be invoked.
The set of candidate instance constructors consists of all accessible
instance constructors contained in the direct base class. If this set is
empty, or if a single best instance constructor cannot be identified, a
compile-time error occurs.

For more information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_10_10_1.asp

====================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
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.
 
M

Marty McDonald

Thanks for the information. I looked at the C# spec and read section 10.10.
Most applicable to my issue was section 10.10.5:
10.10.5 Private constructors
When a class declares only private instance constructors, it is not possible
for other classes to derive from the class or create instances of the class
(an exception being classes nested within the class). Private instance
constructors are commonly used in classes that contain only static
members....

Thanks everyone, you helped resolve the issue!
 
J

Jeffrey Tan[MSFT]

Hi Marty,

Thanks for your feedback.

I am glad our reply makes sense to you.

Yes, actually, you just do Private Constructor in your code, so you can not
inherited this class.

If you have any further concern, please feel free to post in this group. I
will work with you.

Best regards,
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.
 

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