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
(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