Thomas
Thomas

Reputation: 6196

How do I make a private nested class that can be used as a public field in the outer class?

Example:

public class BoundingBox
{
    public Vector3Double Positon { get; set; }
    public Vector3Double Apothem { get; set; }
    public ExtremasForX X;

    public BoundingBox(Vector3Double position, Vector3Double size)
    {
        Positon = position;
        X = new ExtremasForX(this);

    }

    private class ExtremasForX
    {
        private BoundingBox box;
        public ExtremasForX(BoundingBox box)
        {
            this.box = box;
        }
        public double Max
        {   
            get { return box.Positon.X + box.Apothem.X ; }
        }
        public double Min
        {
            get { return box.Positon.X - box.Apothem.X; }
        }



    }
}

This code produces an accessibility error: BoundingBox.X has a higher level than it's type.

I would like an inner class that does not have a public constructor, as I only wish to use the class as a namespace for the outer class. How can I do it?

Upvotes: 0

Views: 83

Answers (2)

Jashaszun
Jashaszun

Reputation: 9270

Change the access modifier of class ExtremasForX to public and change its constructor to internal instead of public, like so:

public class BoundingBox
{
    public Vector3Double Positon { get; set; }
    public Vector3Double Apothem { get; set; }
    public ExtremasForX X;

    public BoundingBox(Vector3Double position, Vector3Double size)
    {
        Positon = position;
        X = new ExtremasForX(this);

    }

    public class ExtremasForX
    {
        private BoundingBox box;
        internal ExtremasForX(BoundingBox box)
        {
            this.box = box;
        }
        public double Max
        {   
            get { return box.Positon.X + box.Apothem.X ; }
        }
        public double Min
        {
            get { return box.Positon.X - box.Apothem.X; }
        }



    }
}

Upvotes: 2

Craig Gidney
Craig Gidney

Reputation: 18316

If you really don't want to expose the inner type, you can to have the inner class implement an interface. Then, in the outer class, you expose X as being of the interface type but internally use the inner class' type.

Personally, I would just make the inner class public. Users can't hurt anything by instantiating the class, so it's not a big deal to expose the constructor.

Code for exposing the inner type, without exposing the constructor, via an interface:

public class BoundingBox
{
    public Vector3Double Positon { get; set; }
    public Vector3Double Apothem { get; set; }
    public IExtremasForX X { get { return _x; } }
    private ExtremasForX _x;

    public BoundingBox(Vector3Double position, Vector3Double size)
    {
        Positon = position;
        _x = new ExtremasForX(this);
    }
    public interface IExtremasForX {
        public double Max { get; }
        public double Min { get; }
    }

    private class ExtremasForX : IExtremasForX
    {
        private BoundingBox box;
        public ExtremasForX(BoundingBox box)
        {
            this.box = box;
        }
        public double Max
        {   
            get { return box.Positon.X + box.Apothem.X ; }
        }
        public double Min
        {
            get { return box.Positon.X - box.Apothem.X; }
        }
    }
}

Upvotes: 6

Related Questions