The return value cannot be changed because it is not a variable

I have a class called BaseRobot :

  var robot2 = new BaseRobot(0, 0, 0); private Point mHome; public Point Home { get { return mHome; } } 

This is where the original house is created, I want to create a new house in program.cs . I have the following code, but it does not work, an error appears with a message

It is not possible to change the return value because it is not a variable.

code:

  robot2.Home.X = 1 robot2.Home.Y = 5; { Console.WriteLine("===New robot at specified home position==="); StringBuilder ab = new StringBuilder(); ab.AppendFormat("Robot#2 has home at <{0},{0}>.\r\n ", robot2.Home.X, robot2.Home.Y); ab.AppendFormat("It is facing {0} ", robot2.Orientation); ab.AppendFormat("and is currently at <{0},{0}>.\r\n", robot2.Position.X, robot2.Position.Y); Console.WriteLine(ab.ToString()); } 

How to assign new values ​​x and Y?

+6
source share
7 answers

You need to set the Home property directly, it is usually best to create a new Point object ...

 robot2.Home = new System.Drawing.Point(1, 5);//x, y 

Also, to allow you to apply the set accessory to your Home ... property

 public Point Home { get { return mHome; } set { mHome = value; } } 

If you want to know more information about why the compiler won't let you assign a value directly to the X property, then check out a few answers here

+19
source

You should change your code as follows:

 private Point mHome; public Point Home { get { return mHome; } set { mHome = value; } } 

and install it like this:

 robot2.Home = new Point(1, 5); 

Structures are immutable, so changing the value actually returns a new instance of Point , but your property does not have setter.

+5
source

You need to add a set accessory to the Home property:

  private Point mHome; public Point Home { get { return mHome; } set { mHome = value; } } 
+2
source

System.Drawing.Point is a value type, that is, your Home property returns a copy of the private mHome , not a reference to it.

You will need to add a setter for the Home property and adjust the code:

  private Point mHome; public Point Home { get { return mHome; } set {mHome = value;} } 

Adjust your calling code to assign a new Home item:

  robot2.Home = new Point(1, 5); 
+2
source

In addition to the already mentioned solution for creating a public device, you can also:

Creating customization methods public void SetHomeX(double x) { mHome.X = x; } public void SetHomeY(double y) { mHome.Y = y; } public void SetHomeX(double x) { mHome.X = x; } public void SetHomeY(double y) { mHome.Y = y; }

Open field as public public Point Home; Note: Publication of a public announcement is usually not considered the best solution. But it can be convenient in some situations.

+1
source

Somewhere I came across this good explanation (I will adapt it for this current case):

This is a bad part of C # design.

Inside, things like robot2.Home are implemented as properties, so when you try to assign a value to Home.X , the get-function for Home is called under the hood (and this is where “Cannot change return value ..” comes from). When this is done, you cannot assign the Point structure to a separate member ( X ), but you must assign the entire structure.

So in C # you need to read the value in Point and then change it:

 robot2.Home = robot2.Home + new Point (deltaX, deltaY); 

Or (if we are not interested in the previous value (as in this case)) simply assign it to a new one:

 robot2.Home = new Point (1f, 5f); 

PS: Of course, you also need to add a setter for the Home property (as mentioned in other answers).

+1
source

Assume that properties X and Y are probably only getters, such as the property "Home".

Is there a function in your robot class to move a position? Use this. if you do not provide a complete example, please

0
source

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


All Articles