I am getting a "Protection Level Error" when I access a private variable
(numberOfEngine s) from my subclass SingleEnginePla ne. 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.NumberO fEngines (property)
and not
myPlane._number OfEngines (variable)
But at the same time be able to access _numberOfEngine s directly from the
subclass.
*************** *************** *****
using System;
namespace ConsoleApplicat ion13
{
public abstract class Plane
{
public string tailNumber;
private int _numberOfEngine s;
public string TailNumber
{
get
{
return tailNumber;
}
set
{
tailNumber = value;
}
}
public int NumberOfEngines
{
get
{
return _numberOfEngine s;
}
set
{
_numberOfEngine s = value;
}
}
}
public class SingleEnginePla ne : Plane
{
public SingleEnginePla ne()
{
_numberOfEngine s = 1; <--- the error
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
SingleEnginePla ne myPlane = new SingleEnginePla ne();
myPlane.TailNum ber = "150";
Console.WriteLi ne(String.Forma t("My 1st Planes Tail Number is: {0}",
myPlane.TailNum ber));
Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
",myPlane._numb erOfEngines));
Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
",myPlane.Numbe rOfEngines));
Console.Read();
}
}
}
*************** *************** *************** *************** *******
Thanks,
Tom
(numberOfEngine s) from my subclass SingleEnginePla ne. 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.NumberO fEngines (property)
and not
myPlane._number OfEngines (variable)
But at the same time be able to access _numberOfEngine s directly from the
subclass.
*************** *************** *****
using System;
namespace ConsoleApplicat ion13
{
public abstract class Plane
{
public string tailNumber;
private int _numberOfEngine s;
public string TailNumber
{
get
{
return tailNumber;
}
set
{
tailNumber = value;
}
}
public int NumberOfEngines
{
get
{
return _numberOfEngine s;
}
set
{
_numberOfEngine s = value;
}
}
}
public class SingleEnginePla ne : Plane
{
public SingleEnginePla ne()
{
_numberOfEngine s = 1; <--- the error
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
SingleEnginePla ne myPlane = new SingleEnginePla ne();
myPlane.TailNum ber = "150";
Console.WriteLi ne(String.Forma t("My 1st Planes Tail Number is: {0}",
myPlane.TailNum ber));
Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
",myPlane._numb erOfEngines));
Console.WriteLi ne(String.Forma t("My 1st Plane has {0} engines:
",myPlane.Numbe rOfEngines));
Console.Read();
}
}
}
*************** *************** *************** *************** *******
Thanks,
Tom
Comment