C# 无法修改返回值,因为它不是变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18126289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot modify the return value because it is not a variable
提问by user2657462
I have a class called BaseRobot
:
我有一个班级叫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
那就是创建原始家的地方,我想在program.cs
. 我有以下代码但它不起作用,它出现了一个错误说
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?
您如何为 x 和 Y 分配新值?
回答by weenoid
You need to add a set
accessor to the Home
property:
您需要set
为该Home
属性添加一个访问器:
private Point mHome;
public Point Home
{
get { return mHome; }
set { mHome = value; }
}
回答by musefan
You need to set the Home
property directly, usually best to create a new Point
object...
需要Home
直接设置属性,通常最好新建一个Point
对象...
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...
此外,为了允许您需要将set
访问器应用于您的Home
财产......
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
回答by pdriegen
System.Drawing.Point
is a value type, meaning your Home
property is returning a copyof the private mHome
, rather than a reference to it.
System.Drawing.Point
是值类型,这意味着您的Home
属性返回的是 private的副本mHome
,而不是对它的引用。
You'll have to add a setter for the Home property and adjust your code to:
您必须为 Home 属性添加一个 setter 并将代码调整为:
private Point mHome;
public Point Home
{
get { return mHome; }
set {mHome = value;}
}
The adjust your calling code to assign a new point to Home:
调整您的呼叫代码以将新点分配给 Home:
robot2.Home = new Point(1, 5);
回答by gzaxx
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.
结构是不可变的,因此更改值实际上会返回新Point
实例,但您的属性没有 setter。
回答by lordkain
Its a wil guess , but the property X and Y are probably only getters, like Your Home property.
这是一个随意的猜测,但属性 X 和 Y 可能只是 getter,就像 Your Home 属性一样。
Is there a function in your robot class to move the position?? Use that. if not provide full example please
你的机器人类中是否有移动位置的功能?用那个。如果没有提供完整的例子,请
回答by xmedeko
Beside the already mentioned solution to make a public property setter, you may also:
除了已经提到的制作公共属性设置器的解决方案之外,您还可以:
Create setter methodspublic void SetHomeX(double x) { mHome.X = x; }
public void SetHomeY(double y) { mHome.Y = y; }
创建 setter 方法public void SetHomeX(double x) { mHome.X = x; }
public void SetHomeY(double y) { mHome.Y = y; }
Expose the field as publicpublic Point Home;
Note:exposing a filed as public is usually considered as not a nice solution. But may be handy in some situations.
将字段公开为 publicpublic Point Home;
注意:将字段公开为 public 通常被认为不是一个好的解决方案。但在某些情况下可能会很方便。
回答by user1234567
I've met somewhere this good explanation (I'll adapt it for this current case):
我在某个地方遇到过这个很好的解释(我将针对当前情况对其进行调整):
It's a bad part of the C# design.
这是 C# 设计的一个糟糕部分。
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.
在内部,类似的东西robot2.Home
是作为属性实现的,因此当您尝试为 分配值时Home.X
,会在Home
后台调用get 函数(这就是“无法修改 .. 的返回值”的来源)。完成后,您不能分配给结构的单个成员 ( X
) Point
,而必须分配整个结构。
So in C#, you need to read the value out into a Point
and then modify it:
所以在 C# 中,你需要将值读入 aPoint
然后修改它:
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).
PS:当然,您还需要为 Home 属性添加一个 setter(如其他答案中所述)。