First I thought it had something to do with that new "automatic properties" feature of C#, but when I used the normal scheme for properties I got the same error message : Cannot modify the return value of 'AxisTest.Form1.Axis.XOrg' because it is not a variable
Wonder what is wrong here.
I tried a few things, my code is a bit of a mess but here it is :

namespace AxisTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics G = e.Graphics;
            Axis Ax = new Axis();
            Pen P = new Pen(Ax.LineColor);

            //***error*** Ax.Origin.X = 50;
            //***error*** Ax.XOrg.X = 50F;
           
            G.DrawLine(P, (float)(50.0 + Ax.Minimum), (float)50.0, 
                (float)(50.0 + Ax.Maximum), (float)50.0);
        }

        class Axis
        {
            private PointF _XOrg = new PointF(50, 50);

            public Axis()
            {
                LineColor = Color.Black;
                Minimum = 0;
                Maximum = 100;
                PrimaryTicUnit = 10;
                SecondaryTicUnit = 5;
                //***error*** this.Origin.X = 50;
                //***error*** Origin.Y = 50;
            }

            public Color LineColor { get; set; }
            public float Minimum { get; set; }
            public float Maximum { get; set; }
            public float PrimaryTicUnit { get; set; }
            public float SecondaryTicUnit { get; set; }
            public PointF Origin { get; set; }

            public PointF XOrg
            {
                get { return _XOrg; }
                set { _XOrg = value; }
            }    
        }
    }
}

Recommended Answers

All 7 Replies

Try this:

Ax.Origin = new PointF(50, 50F);

Thanks Scott that works, but I still have the same error when I try to change X or Y fields.

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics G = e.Graphics;
            Axis Ax = new Axis();
            Pen P = new Pen(Ax.LineColor);

            Ax.Origin.X = 55; //still same error

            G.DrawLine(P, (float)(Ax.Origin.X/*OK Ax.XOrg is also ok*/ + Ax.Minimum), (float)50.0, 
                (float)(50.0 + Ax.Maximum), (float)50.0);
            Ax.XOrg.X = 55; //still same error
        }

        class Axis
        {
            private PointF _XOrg = new PointF(50, 50);

            public Axis()
            {
                LineColor = Color.Black;
                Minimum = 0;
                Maximum = 100;
                PrimaryTicUnit = 10;
                SecondaryTicUnit = 5;
                Origin = new PointF(50, 50); //OK
            }

            public Color LineColor { get; set; }
            public float Minimum { get; set; }
            public float Maximum { get; set; }
            public float PrimaryTicUnit { get; set; }
            public float SecondaryTicUnit { get; set; }
            public PointF Origin { get; set; }

            public PointF XOrg
            {
                get { return _XOrg; }
                set { _XOrg = value; }
            }    
        }

That is because PointF is a struct. I'll use forms for an example:

public partial class FormCallback : Form, IClearTextBox
  {
    private Point _pt;
    public Point PT
    {
      get { return _pt; } //returns a COPY of the struct
      set { _pt = value; }
    }
    
    public FormCallback()
    {
      InitializeComponent();
      this.PT = new Point();
      PT.X = 5; //PT. returns a copy, then you set the copies X to 5. The changes are not 
      //retained thus the compiler stops you from doing it because you want to change
      //the original, but you're actually changing a copy.
    }

Since the Point is a property and you're returning it with get { return X; } it is actually returning a copy of the struct. The compiler is stopping you from making changes to a copy of a struct since you meant to modify the original value. Can you imagine if it let you do this how perplexed you would be figuring out why the value did not set? :(

If you expose the field directly then you can modify the value:

public partial class FormCallback : Form, IClearTextBox
  {
    public Point _pt;
    public Point PT
    {
      get { return _pt; } //returns a COPY of the struct
      set { _pt = value; }
    }
    
    public FormCallback()
    {
      InitializeComponent();
      this.PT = new Point();
      //This works
      _pt.X = 90; 
    }

I can understand that completely.
I'm learning here Scott, thanks.
But Color is also a struct and when I change that in like Ax.LineColor = Color.Red; I get no error, how come?

Because you're assigning it a new struct.

public struct Color
{
  public static Color AliceBlue { get; }
  public static Color AntiqueWhite { get; }
}

As you can see above they have static members declare inside of the struct to keep the .NET framework organized. The properties for Color.R or Color.G are all read-only but if they had a set accessor you would get the error when you called this.Color.R = 5; since the Get() access returns a copy. In this case you are assigning a new instance however.

Scott, again, you made my day!
Thank you very much!

You're welcome and good luck! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.