Protection Level

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I am getting a "Protection Level Error" when I access a private variable
(numberOfEngines) from my subclass SingleEnginePlane. If I set it as public
I can access it directly from my object in my Main function.

I would like the subclass to access it directly but not directly from my
function.

I would like to access it by:

myPlane.NumberOfEngines (property)

and not

myPlane._numberOfEngines (variable)

But at the same time be able to access _numberOfEngines directly from the
subclass.

***********************************
using System;

namespace ConsoleApplication13
{
public abstract class Plane
{
public string tailNumber;
private int _numberOfEngines;
public string TailNumber
{
get
{
return tailNumber;
}
set
{
tailNumber = value;
}
}
public int NumberOfEngines
{
get
{
return _numberOfEngines;
}
set
{
_numberOfEngines = value;
}
}
}

public class SingleEnginePlane : Plane
{
public SingleEnginePlane()
{
_numberOfEngines = 1; <--- the error
}
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SingleEnginePlane myPlane = new SingleEnginePlane();

myPlane.TailNumber = "150";

Console.WriteLine(String.Format("My 1st Planes Tail Number is: {0}",
myPlane.TailNumber));
Console.WriteLine(String.Format("My 1st Plane has {0} engines:
",myPlane._numberOfEngines));
Console.WriteLine(String.Format("My 1st Plane has {0} engines:
",myPlane.NumberOfEngines));
Console.Read();
}
}
}
*******************************************************************

Thanks,

Tom
 
tshad said:
[...]
But at the same time be able to access _numberOfEngines directly from the
subclass.

Then the member variable needs to be "protected" rather than "private".

Pete
 
tshad,

I think you mean you want to access it using myPlace._numberOfEngines,
as you can already access it from the property.

If you want to access the variable, you have to change the access
modifier of the variable from private to internal or protected.
 
tshad said:
Peter Duniho said:
tshad said:
[...]
But at the same time be able to access _numberOfEngines directly from the
subclass.

Then the member variable needs to be "protected" rather than "private".

That was it.

However, that's usually a bad idea, IMO. I prefer to make all fields
private, and use properties even for derived class access.
 
You can always access the property because that's a public property.
In that case it doesn't mather.

it's also posible to combine the properties and access modifiers:

just like:

public int NumberOfEngines
{
get
{
return _numberOfEngines;
}
protected set
{
_numberOfEngines = value;
}
}

in that case the protection level of the Set is lower than the Get.
 
You can always access the property because that's a public property.
In that case it doesn't mather.

it's also posible to combine the properties and access modifiers:

<snip>

Note that this is C# 2 specific. In C# 1 the get/set had to have the
same access.

Jon
 
active T said:
You can always access the property because that's a public property.
In that case it doesn't mather.

it's also posible to combine the properties and access modifiers:

just like:

public int NumberOfEngines
{
get
{
return _numberOfEngines;
}
protected set
{
_numberOfEngines = value;
}
}

in that case the protection level of the Set is lower than the Get.

That's interesting.

I hadn't seen that before.

Thanks,

Tom
 
Back
Top