Discussion:
C# protected constructor question
(too old to reply)
kmrneedsnethelp
2007-06-06 17:46:00 UTC
Permalink
I have the class structure shown below. When I compile I receive the
following erros:
'B.B()' is inaccessible due to its protection level
'C.C()' is inaccessible due to its protection level

I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.

Can anyone explain it?

Thanks in advance!

abstract class A
{
public enum AType {BType, CType};
protected A()
{
}
public static A CreateAFactory(AType typ)
{
switch(typ)
{
case AType.BType:
return new B();
case AType.CType:
return new C();
}
}
}
class B : A
{
protected B() : base()
{
}
}
class C : A
{
protected C() : base()
{
}
}
AlexS
2007-06-06 18:02:46 UTC
Permalink
I believe you can't protect default constructor as you do in B and C and use
it in A. If you change C() and B() to public it will work.

HTH
Alex
Post by kmrneedsnethelp
I have the class structure shown below. When I compile I receive the
'B.B()' is inaccessible due to its protection level
'C.C()' is inaccessible due to its protection level
I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.
Can anyone explain it?
Thanks in advance!
abstract class A
{
public enum AType {BType, CType};
protected A()
{
}
public static A CreateAFactory(AType typ)
{
switch(typ)
{
return new B();
return new C();
}
}
}
class B : A
{
protected B() : base()
{
}
}
class C : A
{
protected C() : base()
{
}
}
Jon Skeet [C# MVP]
2007-06-06 18:08:39 UTC
Permalink
Post by kmrneedsnethelp
I have the class structure shown below. When I compile I receive the
'B.B()' is inaccessible due to its protection level
'C.C()' is inaccessible due to its protection level
I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.
Can anyone explain it?
The constructors for B and C are protected, which means they can only
be called from classes *derived* from them. A doesn't derive from B or
C, therefore it can't call protected members of B or C.
--
Jon Skeet - <***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Peter Duniho
2007-06-06 18:12:28 UTC
Permalink
On Wed, 06 Jun 2007 10:46:00 -0700, kmrneedsnethelp
Post by kmrneedsnethelp
I have the class structure shown below. When I compile I receive the
'B.B()' is inaccessible due to its protection level
'C.C()' is inaccessible due to its protection level
I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.
Can anyone explain it?
You may want to read this:
http://msdn2.microsoft.com/en-us/library/wxh6fsc7.aspx

As for your specific question, the "protected" access modifier allows
derived classes to access base class members, but it doesn't work the
other way. In your example, you're trying to access protected members in
the derived classes, from the base class. The members need to be public
for A to access the constructors.

Pete
Peter Duniho
2007-06-06 18:19:22 UTC
Permalink
On Wed, 06 Jun 2007 10:46:00 -0700, kmrneedsnethelp
Post by kmrneedsnethelp
[...]
I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.
By the way, you can probably accomplish the general idea of what you're
trying to do by using the "internal" access modifier. This will allow A
to access B and C constructors (assuming they remain in the same assembly)
while hiding them from other code that may use the classes.

Pete
kmrneedsnethelp
2007-06-08 01:22:01 UTC
Permalink
Thanks to all who responded. I took Peter's advice and was able to implement
my class relationships using the internal keyword. Here is the result:
abstract class A
{
public enum AType {BType, CType};
protected A()
{
}
public static A CreateAFactory(AType typ, int x)
{
switch(typ)
{
case AType.BType:
return new B(x, "B");
case AType.CType:
return new C(x, "C", "D");
}
return null;
}
}
class B : A
{
internal B() : base()
{
}
}
class C : A
{
internal C() : base()
{
}
}
Post by Peter Duniho
On Wed, 06 Jun 2007 10:46:00 -0700, kmrneedsnethelp
Post by kmrneedsnethelp
[...]
I am sure it is because of the protected keyword on the constructors for B
and C but I do not understand the underlying reason.
By the way, you can probably accomplish the general idea of what you're
trying to do by using the "internal" access modifier. This will allow A
to access B and C constructors (assuming they remain in the same assembly)
while hiding them from other code that may use the classes.
Pete
Loading...