0

I'm creating a simply User Control. I have 4 Points and am trying to fill the area of the points using e.Graphics.FillPolygon(brush, shape);

shape is created using Point[] shape = { Target, PointB, PointC, PointD };

Those points come from the following:

    Point target = new Point(0, 0);
    public Point Target {
        get { return target; }
        set { target = value; }
    }

    Point pointB = new Point(100, 0);
    public Point PointB { get; set; }
    //    get { return pointB; }
    //    set { pointB = value; }
    //}


    Point pointC = new Point(0, 100);
    public Point PointC {
        get { return pointC; }
        set { pointC = value; }
    }


    Point pointD = new Point(200, 500);
    public Point PointD {
        get { return pointD; }
        set { pointD = value; }
    }

My problem is that using public Point PointB { get; set; } seems to not be working, so instead I have to write out the entire get { return pointB; } set { pointB = value; }

Is there something peculiar about the shorthand notation?

Using { get; set; }: enter image description here

Using the longer (proper?) notation: enter image description here

It appears as though it just ignores PointB when using the shorthand notation.



Also, is it proper to have Point target = new Point(0, 0); before or after the Accessor bit:

Point target = new Point(0, 0);
public Point Target {
    get { return target; }
    set { target = value; }
}

or

public Point Target {
    get { return target; }
    set { target = value; }
}
Point target = new Point(0, 0);
Tawm
  • 535
  • 3
  • 12
  • 25

1 Answers1

1

The initialization should look like this (newer C# versions, C# 6.0 or higher):

public Point PointB { get; set; } = new Point(100, 0);

When you do:

Point pointB = new Point(100, 0);     // Never used, not a backing field.
public Point PointB { get; set; }     // Has a backing field that you cannot refer to.

the private pointB is never used. The backing field of the auto-property is not called pointB. It has an unusable name, and you can never reach it without using the property.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • Was there a time when my way was correct? I've recently started learning C# and found other people's projects which used what I have, which is why I've been using it. Also, does that mean there is simply no `pointB`, only `PointB`? This question has the same notation: https://stackoverflow.com/questions/1310223/what-does-this-mean-public-name-get-set?noredirect=1&lq=1 – Tawm Aug 06 '17 at 00:23
  • No, your way has never been correct. When you have an auto-property `public Point PointB { get; set; }`, there does exist a backing field, but its true name is weird and unpredictable and chosen by the C# compiler, and even if you happen to know what it is, it is not allowed to refer to it. In the thread you mention, the answer by Bryan Watts has a comment by Jon Skeet where it says: _... although the name of the field is actually an "unspeakable" name (e.g. `<>__name`) which is invalid as a C# identifier. This stops you from directly accessing the field from your code._ – Jeppe Stig Nielsen Aug 06 '17 at 09:59
  • Okay, so I'm having difficulties trying to use the Accessors within the same UserControl.cs. I feel this is the incorrect way of doing this, but I've got `public PointF PointB { get; set; }` at the top, inside the class. Then I have methods further down which I use to actually use it like `public void PointConstructor() { PointB = new PointF(0, 0); triangle[1] = PointB; }` I'm only doing this because I was having a tough time doing anything with `PointB` just in the regular `class UserControl{ }` – Tawm Aug 06 '17 at 14:47
  • @Tawm Code must go into methods or constructors etc., it cannot "float" at the `class` level. When there is a return type, like `void` in your comment here, it is a method. When there is no return type and the name is identical to the name of the class, you have a constructor, as in `public Form2() { PointB = new PointF(0, 0); triangle[1] = PointB; }` inside a `class` called `Form2`. – Jeppe Stig Nielsen Aug 07 '17 at 07:15