Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

C# protected constructor question


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

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

"kmrneedsnethelp" <kmrneedsneth@discussions.microsoft.com> wrote in
message news:584BB3D5-0538-4636-8D4A-7DD6D7753A19@microsoft.com...

kmrneedsnethelp <kmrneedsneth@discussions.microsoft.com> wrote:
> 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.

--
Jon Skeet - <s@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

-----------------------------------------------Reply-----------------------------------------------

On Wed, 06 Jun 2007 10:46:00 -0700, kmrneedsnethelp  

<kmrneedsneth@discussions.microsoft.com> wrote:
> 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

-----------------------------------------------Reply-----------------------------------------------

On Wed, 06 Jun 2007 10:46:00 -0700, kmrneedsnethelp  

<kmrneedsneth@discussions.microsoft.com> wrote:
> [...]
> 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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc