0

I have a .cs file which looks like the following

namespace TarkovMapper.ClassObjects
{
    class PointCloud_Object
    {
        public void AddPoint(PointEntry_Object point)
        {
            PointLayer pointLayer = LoadPointLayer(path);
            pointLayer.Points[point.Location_x,point.Location_y]++;
        }
        private PointLayer LoadPointLayer(string path)
        {
            if (!File.Exists(path)) return new PointLayer(this.Width, this.Height);
            Stream s = File.OpenRead(path);
            BinaryFormatter b = new BinaryFormatter();
            PointLayer returnObject = (PointLayer) b.Deserialize(s);
            s.Close();
            return returnObject;
        }
    }
    [Serializable]
    class PointLayer
    {
        public PointLayer(int width, int height)
        {
            this.Points = new int[width, height];
        }
        public int[,] Points { get; private set; } // <- private set!!!
        public int Maximum { get; private set; }
    }
}

My Question is regarding the Variable "Points" in the class PointLayer. Eventhough I have the Modifier private set; the following line in PointCloudObject is no issue pointLayer.Points[point.Location_x,point.Location_y]++;.

why is that?

1 Answer 1

2

The modifier refers to the Points array, not the array's individual elements. The PointCloud_Object class cannot assign a new array to the PointLayer.Points variable, but it can manipulate the individual array elements.

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh thank you! Is there a way I can protect the elements inside the Array so they cant be modified from outside the Class?
I don't think you can protect the elements directly as long as the array is exposed. One option would be to make the array private and provide a method that returns the individual elements. Another option might be to use an alternate data structure instead of an array (maybe ReadOnlyCollection would meet your needs).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.