Jon Butler Jon Butler - 25 days ago 5
C# Question

can't access due to protection level c# class

I'm building a few classes with inheritance working with deriving class methods and I'm getting a protection issue in my application. Here is a little bit of the source of one of the classes that is getting the error..

CODE:

class Cone : Circle
{
private double height; // Data members

public Cone() : base() // Default constructor with explicit call
{ // to base (Circle) default constructor
this.Label = "Unlabeled Cone";
this.height = 0.0;
}



public Cone(Point centerValue, double radiusValue, double heightValue):
base(centerValue, radiusValue) // Initializing constructor with explicit
{ // call to base (Circle) initializing construtor
this.Label = "Unlabeled Cone";
this.Height = heightValue;
}

public Cone(Point centerValue, double radiusValue, double heightValue, string labelValue):
base(centerValue, radiusValue, labelValue) // Initializing constructor with explicit
{ // call to base (Circle) initializing construtor
this.Height = heightValue;
}

public Cone(Cone sourceCone) // Copy constructor
{
this.Copy(sourceCone);
}

public double Height // Define read/write Height property
{
get
{
return this.height;
}
set
{
if (value >= 0.0)
this.height = value;
else
{
Console.WriteLine("Runtime error: {0} can not be assigned to a Height property\n", value);
ConsoleApp.Exit();
}
}
}


Here is the class it is derived from:

public class Circle : object
{

private Point center; // Data member
private double radius; // Data member
private string label; // Data member

public Circle()
{
this.center = new Point();
this.radius = 0.0;
this.label = "Unlabeled Circle";
}

public Circle(Point centerValue, double radiusValue)
{
this.center = new Point(centerValue);
this.Radius = radiusValue;
this.label = "Unlabeled Circle";
}

public Circle(Point centerValue, double radiusValue, string labelValue)
{
this.center = new Point(centerValue);
this.Radius = radiusValue;
this.Label = labelValue;
}

public Circle(Circle sourceCircle) // Copy constructor
{
this.center = new Point();
this.Copy(sourceCircle);
}

public Point Center // Define read/write Center property
{
get
{
return this.center;
}
set
{
this.center = value.Clone();
}
}


the classes are way longer to add in volume, and area and what not, but in my console app I keep getting that it cannot access Cone due to its protection level. I'm struggling to see where something is private that is keeping it from being able to be accessed from the console app. If you would like to see full class code, here it is in pastebin... sourcecode

Answer

Your class declaration doesn't have an accessibility level:

class Cone : Circle

if you don't provide an access level, it will default to the least accessibility level (most restricted) it can. Use:

public class Cone