Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

c# - Cannot modify the return value 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; }
  }

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}>.
 ", robot2.Home.X, robot2.Home.Y);
                ab.AppendFormat("It is facing {0} ", robot2.Orientation);
                ab.AppendFormat("and is currently at <{0},{0}>.
", robot2.Position.X, robot2.Position.Y);
                Console.WriteLine(ab.ToString());
            }

How do you assign new values to x and Y?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...