Extract class attributes with accessor

I do not know the correct technical terms to describe my question, so I will give an example:

    private Point _PrivateVect = new Point();
    public Point Publicvect
    {
        get
        {
            return _PrivateVect;
        }
        set
        {
            _PrivateVect = value;
        }
    }

The problem is that if I wanted to access Publicvect.X, I got an error Cannot modify the return value of 'Publicvect' because it is not a variable. Is there any way around this? Or do I just need to do Publicvect = new Point(NewX, Publicvect.Y);forever?

+3
source share
2 answers

Another reason why mutable structures are evil. One way is to expose dimensions as accessories for convenience:

public Point PublicX {
    get {return _PrivateVect.X;}
    set {_PrivateVect.X = value;}
}
public Point PublicY {
    get {return _PrivateVect.Y;}
    set {_PrivateVect.Y = value;}
}

, ; , new Point(x,y) , Point . , , , , .

+2

, - Value Type. , Pointvect.X, , , , .

+1

Source: https://habr.com/ru/post/1757445/


All Articles