-1

Consider the following code.

using System.Drawing;

public class Element
{
    public static Point position1;
    private static Point position2;

    public static Point Position2
    {
        get { return position; }
        set { position = value; }
    }
}

class Drawing
{
    public Drawing()
    { 
        Element.position1.X = 0; //Correct
        Element.Position2.X = 0; //Incorrect
    }
}

Why can not the value of position2.X be changed through Element.Position2.X?

Because of this I am obliged to do :

Point newPoint = new Point (0, 0);
Element.Position2 = newPoint;
//Or : Element.Position2 = new Point (0, Element.Position2.Y);
  • Why is not it simply possible to access the value of position2.X as with position1.X but using Element.Position2.X ?
  • Do you know any other way to solve the problem?
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Een Amok
  • 107
  • 2
  • 10
  • 1
    Because `Point` is a `struct` and not a `class` – stuartd May 06 '17 at 21:21
  • There not actually being a `position` field could cause access issues, I imagine. – Abion47 May 06 '17 at 21:21
  • 1
    Are you sure your code compiles? `position` variable not declared – Fabio May 06 '17 at 21:22
  • Getter will return copy of your original encapsulated member. That's why creating new Point works, but updating will update a copy, not original value. – Fabio May 06 '17 at 21:24
  • It's amazing what you'll find, if you just [search Stack Overflow](https://stackoverflow.com/search?q=%5Bc%23%5D+cannot+modify+because+not+a+variable) for the error message the compiler gives you when you try to do something wrong. As explained in the many duplicate questions already on SO, your property `Position2` isn't returning a variable; it's returning a temporary copy of the value, so modifying the `X` property of that value will have no effect. The "other way to solve the problem" is exactly as you already know: assign the entire value back. – Peter Duniho May 06 '17 at 21:54
  • Oh okay, I'll do it from now on, I'll think about the error message that the compiler gives. - @PeterDuniho – Een Amok May 06 '17 at 23:22
  • "Updating will update a copy, not original value." Can you explain please ? I do not understand well. - @Fabio – Een Amok May 07 '17 at 00:19
  • "Are you sure your code compiles? position variable not declared" Sure it does. I have also thought about it and tried the code and it compiles very well. I don't know why but I think it works because of the `static` modifier. – Een Amok May 07 '17 at 00:29

1 Answers1

-2

Correctly define the public or private operator

  public class Element
    {
        private static Point position1;
        private static Point position2;

        public static Point Position1
        {
            get { return position1; }
            set { position1 = value; }
        }
        public static Point Position2
        {
            get { return position2; }
            set { position2 = value; }
        }
    }
Rovann Linhalis
  • 601
  • 8
  • 14
  • This is not an answer to the question that was asked. The OP is trying to understand why the compiler won't let them modify `Position2.X`. The question has nothing to do with `public` vs `private` – Peter Duniho May 06 '17 at 21:55