468,720 Members | 1,607 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

why member inaccessible?

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
Nov 15 '05 #1
6 2554
you cannot inherit a class with a private constructor.. as the inheriting
class cannot call any of its methods..
"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #2
Thanks! Is that true even if the base class will have nothing but static
members?

"Syanide" <me@knowhere.com> wrote in message
news:u3*********************@news-text.cableinet.net...
you cannot inherit a class with a private constructor.. as the inheriting
class cannot call any of its methods..
"Marty McDonald" <mc******@wsdot.wa.gov> wrote in message
news:ug**************@TK2MSFTNGP11.phx.gbl...
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


Nov 15 '05 #3
Marty McDonald wrote:
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.

--
mikeb
Nov 15 '05 #4

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/de...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/de...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.

Nov 15 '05 #5
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!
Nov 15 '05 #6

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.

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by none | last post: by
8 posts views Thread by Ganesh Kundapur | last post: by
2 posts views Thread by Christoph Boget | last post: by
1 post views Thread by Thomas Barnet-Lamb | last post: by
1 post views Thread by =?Utf-8?B?d3BjbWFtZQ==?= | last post: by
2 posts views Thread by tomas | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.