C# protected constructor question

G

Guest

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()
{
}
}
 
A

AlexS

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
 
J

Jon Skeet [C# MVP]

kmrneedsnethelp said:
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?

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.
 
P

Peter Duniho

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?

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
 
P

Peter Duniho

[...]
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
 
G

Guest

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()
{
}
}


Peter Duniho said:
[...]
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
 

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