6

I have a class called BaseRobot:

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

That is where the original home is created, I am wanting to create a new home in the program.cs. I have the following code but it does not work, it is coming up with an error saying

Cannot modify the return value becasue 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 do you assign new values to x and Y?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2657462
  • 121
  • 2
  • 2
  • 7

7 Answers7

20

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

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

Also, in order to allow that you need to apply a set accessor to your Home property...

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

If you want to find out some more information of why the compiler won't let you assignthe value directly to the X property, then check a few of the answers over here

Community
  • 1
  • 1
musefan
  • 47,875
  • 21
  • 135
  • 185
  • I have done this but now getting a message in the main program saying 'Point' could not be found – user2657462 Aug 08 '13 at 12:55
  • @user2657462: Then you likely don't have the correct namespace included. Either add it specifically when creating the new `Point` (see my edit). Or include it at the top of your class file: `include System.Drawing;` (or use "Resolve" in VS) – musefan Aug 08 '13 at 12:57
  • 1
    Yeah this is so obvious when you see it again for the tenth time. Anyway +1. Have a another Good Answer badge. – ouflak Jun 18 '14 at 12:47
7

You have to change your code like this:

private Point mHome;

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

and set it like this:

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

Structs are immutable so changing value in fact returns new Point instance but your property do not have setter.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • 1
    +1 for mentioning that structs are immutable. Was not mentioned in the top voted post, which is important to know why the code in the opening post do not work – Tseng Aug 08 '13 at 14:14
  • 3
    Structs are not immutable, they have value semantics. You can modify a local variable or a field of a struct type unless it's specifically designed to be immutable. – Anton Tykhyy Aug 19 '13 at 16:44
5

I've met somewhere this good explanation (I'll adapt it for this current case):

It's a bad part of the C# design.

Internally, stuff like robot2.Home is implemented as properties, so when you try assign a value to Home.X, a get-function for Home gets called under the hood (and this is where "Cannot modify the return value of .." goes from). When this is done, you can't assign into an individual member (X) of the Point struct, but have to assign the entire struct.

So in C#, you need to read the value out into a Point and then modify it:

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

Or (if we don't interested in previous value (as in this case)) just assign it to new one:

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

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

user1234567
  • 3,991
  • 3
  • 19
  • 25
2

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

   private Point mHome;
   public Point Home
   {
       get { return mHome; }
       set { mHome = value; }
   }
weenoid
  • 1,156
  • 2
  • 11
  • 24
2

System.Drawing.Point is a value type, meaning your Home property is returning a copy of the private mHome, rather than a reference to it.

You'll have to add a setter for the Home property and adjust your code to:

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

The adjust your calling code to assign a new point to Home:

   robot2.Home = new Point(1, 5);
pdriegen
  • 2,019
  • 1
  • 13
  • 19
1

Beside the already mentioned solution to make a public property setter, you may also:

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

Expose the field as public public Point Home; Note: exposing a filed as public is usually considered as not a nice solution. But may be handy in some situations.

xmedeko
  • 7,336
  • 6
  • 55
  • 85
0

Its a wil guess , but the property X and Y are probably only getters, like Your Home property.

Is there a function in your robot class to move the position?? Use that. if not provide full example please

lordkain
  • 3,061
  • 1
  • 13
  • 18
  • Don't forget that if you realize after posting that another answer (that's better or the same) has already been posted, there's no cost to deleting your post in order to avoid cluttering questions with answers that don't add anything. – Michelle Aug 08 '13 at 12:52